Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Need help with OO perl

by dkotturi (Initiate)
on Dec 27, 2008 at 02:02 UTC ( #732734=perlquestion: print w/replies, xml ) Need Help??

dkotturi has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have a small piece of code similar to the one below:
#!/usr/bin/perl package main; use strict; use warnings; my $obj = test2->new(); $obj->get_name; $obj->get_namer; package test2; sub new { my $class = shift; my $self = { lib => undef}; bless($self,$class); my $lib = test1->new(); ($self->{lib}) = ($lib->print_name()); return $self; } sub get_name { my $self = shift; return $self->{lib}->print_name(); } sub get_namer { my $self = shift; return $self->{lib}->print_namer(); } package test1; sub new { my $class = shift; my $self = {}; bless($self,$class); return $self; } sub print_name { my $self = shift; print "From test1","\n"; } sub print_namer { my $self = shift; print "Here is another namer","\n"; }

The above code is saved into a file called 'll.pl'. When I execute the above code as './ll.pl', I get the following message
From test1 Can't call method "print_name" without a package or object reference a +t ./ll.pl line 27.

can you please help me understand what I am doing wrong?

Best Regards,
Dayle Kotturi.

Replies are listed 'Best First'.
Re: Need help with OO perl
by kyle (Abbot) on Dec 27, 2008 at 02:37 UTC

    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.

Re: Need help with OO perl
by borisz (Canon) on Dec 27, 2008 at 02:23 UTC
    ($self->{lib}) = ($lib->print_name()); just put undef into $self->{lib} and undef->print_name can not work later. maybe you should replace
    ($self->{lib}) = ($lib->print_name());
    with:
    $self->{lib} = $lib;
    Boris

      Actually, it's the return value of print "From test1","\n"; that's being returned by print_name() (i.e. 1, not undef) — but otherwise your observation is of course correct...

Re: Need help with OO perl
by xhunter (Sexton) on Dec 27, 2008 at 22:04 UTC

    As many have pointed out the error and alternatives, I want to point out that when debugging it's worth fully mining the error message.

    For example, you posted the error and it said you didn't have an object on line 27 where the method print_name was being called. The object in question is $self->{lib} which should lead one naturally back to where $self->{lib} was set (or defined).

    Looking at the definition of $self->{lib} and knowing it's likely messed up might have shed light on the error; Assigning a return value of a method instead of an object where it seems an object was wanted.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://732734]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2021-01-23 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?