Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Need help with OO perl

by kyle (Abbot)
on Dec 27, 2008 at 02:37 UTC ( [id://732740]=note: print w/replies, xml ) Need Help??


in reply to Need help with OO perl

In test2::new, you say:

my $lib = test1->new(); ($self->{lib}) = ($lib->print_name());

That doesn't cause any error, but it doesn't do what you want it to do. It leaves $self->{lib} == 1, which is what print returns at the end of test1::print_name.

Later, in test2::get_name, you try to use $self->{lib} as an object instance, but it's still just the number 1.

I'd fix this by changing the code in test2::new to:

my $lib = test1->new(); $self->{lib} = $lib; $lib->print_name();

You could also eliminate a variable this way:

$self->{lib} = test1->new(); $self->{lib}->print_name();

You could also "fix" this by having test1::print_name return $self instead of the value of print

sub print_name { my $self = shift; print "I forgot what went here"; return $self; }

I don't like that solution personally since the return value isn't really related to the method, but having methods return $self whenever possible is common enough.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://732740]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-23 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found