Perl Object Creation problem...

H

howa

Hi,

I encountered a problem when using perl's OOP feature"


Directory Layout:
----------------------
c:\temp\perl\test.pl
c:\temp\perl\class\user.pm


user.pm
----------------------
package class::user;

sub new {
my ($class) = @_;
my $objref = {};

bless $objref, $class;
}

sub print_me {
my ($self) = @_;

print "is me!";
}

1;



----------------------
test.pl
----------------------
use lib "C:\\temp\\perl";

use class::user;

$u = new user();
----------------------


and it returns :

Can't locate object method "new" via package "user" (perhaps you forgot
to load
"user"?) at test.pl line 5.

what's wrong with the codes?

thanks...
 
C

Charles DeRykus

howa said:
Hi,

I encountered a problem when using perl's OOP feature"


Directory Layout:
----------------------
c:\temp\perl\test.pl
c:\temp\perl\class\user.pm


user.pm
----------------------
package class::user;

sub new {
my ($class) = @_;
my $objref = {};

bless $objref, $class;
}

sub print_me {
my ($self) = @_;

print "is me!";
}

1;



----------------------
test.pl
----------------------
use lib "C:\\temp\\perl";

use class::user;

$u = new user();
----------------------


and it returns :

Can't locate object method "new" via package "user" (perhaps you forgot
to load
"user"?) at test.pl line 5.

what's wrong with the codes?

I'm not sure what you intended with the parens but `$u = new user()`
gets parsed as 'user->new'. However, your class is 'class::user' not
'user' so you'll need: $u = new class::user which is better expressed
as user::class->new. Customarily, package names start with an upper
case letter too, eg, Class::User. 'Class' is usually not an appropriate
choice for the name either.

You might want to review the OO documentation for more info:

perlboot Perl OO tutorial for beginners
perltoot Perl OO tutorial, part 1
perltootc Perl OO tutorial, part 2
perlobj Perl objects
perlbot Perl OO tricks and examples

hth,
 
H

howa

Aukjan van Belkum 寫é“:
This should be:
$u = new class::user; (or pref.: my $u = new class::user;)
or
$u = class::user->new(); (or pref.: my $u = class::user->new();)


Aukjan.

thx first....

but is it possible to use

$u = new user;

just like in c++, i.e. using namespace std;

sorry as i am really new to perl...
 
B

Brian McCauley

howa said:
Aukjan van Belkum 寫é“:

but is it possible to use

$u = new user;

just like in c++, i.e. using namespace std;

No, there is no such mechanism builtin[1] to Perl5. All class names
must be specified absolutely.

[1] I say builtin because it's just the sort of thing you might expect
to see in an Acme::* modules. Note Acme::* modules are only intended as
jokes.
 
H

howa

Brian McCauley 寫é“:
howa said:
Aukjan van Belkum 寫é“:

but is it possible to use

$u = new user;

just like in c++, i.e. using namespace std;

No, there is no such mechanism builtin[1] to Perl5. All class names
must be specified absolutely.

[1] I say builtin because it's just the sort of thing you might expect
to see in an Acme::* modules. Note Acme::* modules are only intended as
jokes.

it seems a little stupid if the class name must be the package full
name...

anyway, thanks!
 
H

howa

(e-mail address removed)-here.net 寫é“:
Perhaps this will offer enlightenement:

Suppose I have two classes Road::Apple and Fruit::Apple

#/usr/local/bin/howas-private-perl-like-language
use Road::Apple;
use Fruit::Apple;

my $thing = new Apple;

What kind of apple whould $thing be? One is delicious, the other not
so much.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - (e-mail address removed) s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.

in other language, you also have this problem, java or c.

in fact, this is not a problem at all, if you want to specifiy, you can
still use the full path if needed.

the namespace and the class name are not the same thing.
 
T

Tad McClellan

howa said:
(e-mail address removed)-here.net ???


I don't think that was a rhetorical question, but you did not
address it.

What kind of apple whould $thing be?



[snip .sig, you are not supposed to quote those you know]

in other language, you also have this problem, java or c.
^^^^

What is with the "also" there?

Perl does not have that problem.

in fact, this is not a problem at all,


Then what kind of apple whould $thing be?

A red one or a brown one?

If the programmer cannot tell, then it looks like a problem to me...
 
H

howa

Tad McClellan 寫é“:
howa said:
(e-mail address removed)-here.net ???


I don't think that was a rhetorical question, but you did not
address it.

What kind of apple whould $thing be?



[snip .sig, you are not supposed to quote those you know]

in other language, you also have this problem, java or c.
^^^^

What is with the "also" there?

Perl does not have that problem.

in fact, this is not a problem at all,


Then what kind of apple whould $thing be?

A red one or a brown one?

If the programmer cannot tell, then it looks like a problem to me...

did you even have experience on java or c? (no offense)

how they handle namespace/package/import/include ?

of coz perl has more then one way to do it, but seems the way how perl
handle it is not a good solution.
 
S

Stephan Titard

howa escribió:
Tad McClellan 寫é“:
howa said:
(e-mail address removed)-here.net ???

it seems a little stupid if the class name must be the package full
name...

Perhaps this will offer enlightenement:

Suppose I have two classes Road::Apple and Fruit::Apple

#/usr/local/bin/howas-private-perl-like-language
use Road::Apple;
use Fruit::Apple;

my $thing = new Apple;

What kind of apple whould $thing be?

I don't think that was a rhetorical question, but you did not
address it.

What kind of apple whould $thing be?

One is delicious, the other not
so much.

[snip .sig, you are not supposed to quote those you know]

in other language, you also have this problem, java or c.
^^^^

What is with the "also" there?

Perl does not have that problem.

in fact, this is not a problem at all,

Then what kind of apple whould $thing be?

A red one or a brown one?

If the programmer cannot tell, then it looks like a problem to me...

did you even have experience on java or c? (no offense)

how they handle namespace/package/import/include ?

of coz perl has more then one way to do it, but seems the way how perl
handle it is not a good solution.
hi, well
given a problem that could affect many languages the solution depends on
the language and it might even be that the problem disappears for some
language...

the import mecanism in java is there so that essentially you don't have
to type long names, in perl it is not really necessary

for example, the java snippet
import org.vln.*
VeryLongName v = new VeryLongName();

in perl
you save at least the lhs part ;)
my $v = org::vln::VeryLongName->new() ; # better to stick to the arrow...

Also from the point of view of explicit documentation/maintainability I
must prefer to use the full name.

anyway if you really want a short name you can use an alias
via one of the alias modules on CPAN

o use typeglob assignment (which you can read as a way of saying that 2
namespaces are equivalent)
*VLN = *Very::Long::Name;

in the context of OO I use it very little
preferring to hide this behing a module

now perl as a dynamic language has a lot more to offer

you can say
my $class = 'Very::Long::Name';
and
my $v = $class->new;

if you wish

o act on a series of classes

for my $class (@these_classes_that_do_survey_of_the worldcup)
{
my $watcher = $class->new();
$watcher->watch_football();
$watcher->save_result();
}


hth
--stephan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top