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

Hi All,
I have the following piece of code in a larger program.
package Kprocess; sub new { my ($class, $fname) = @_; my $self = { lib => undef }; bless $self, $class; my $lib = Kprocess::Container->new(); my $libfile = Kprocess::File->new($fname); $lib->read($libfile); ($self->{lib}) = ($lib->get_list("library")); return $self; } sub get_value { my ($self, $keyword, $default) = @_; return $self->{lib}->get_value($keyword, $default); }

In the above code, I am confused as to why
return $self->{lib}->get_value($keyword, $default);
works by calling the "get_value" method from the class Kprocess::Container.

Can any one please help me understand?

Thank you.
Mala

Replies are listed 'Best First'.
Re: Perl OO Help: Urgent :)
by jettero (Monsignor) on Jul 31, 2009 at 01:52 UTC
    $obj->get_value calls $self->{lib}->get_value and that was populated with $lib->get_list in list context, so it's calling get_value on the first object returned by get_list.
    ($self->{lib}) = ($lib->get_list("library")); # this is a lot like my @list = $lib->get_list("library"); my ($var) = @list; # only it's storing it in {lib} # and $self->{lib}->get_value # is then rather like $var->get_value # which is rather like $list[0]->get_value

    -Paul

      Hi Paul, Sorry, new to Perl so need some more clarification. In my code, I have the line (please see the code in my question/request)

       ($self->{lib})  = ($lib->get_list("library"));

      what does ($self->{lib}) contain? Thanks, Mala

        After that statement, $self->{lib} contains the first element of the list returned by $lib->get_list('library'). Without knowing what that method returns, no one can be more specific.