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

Hi Folks, I have a package as in:
package Library; sub new { my ($class, $fname) = @_; my $self = { library => undef }; bless $self, $class; my $lib = Data::Container->new(); my $libfile = Data::File->new($fname); $lib->read($libfile); ($self->{library}) = ($lib->get_list("library")); return $self; } sub get_list { my ($self, @keywords) = @_; return $self->{lib}->get_list(@keywords); }

where Data::Container is another package as below:
package Data::Container; sub new { my $class = shift; my $self = { line => shift, items => undef, close => undef, }; $self->{line} = "" unless $self->{line}; bless $self, $class; }

and similarly, Data::File is another package as below:
package Data::File; sub new { my $class = shift; my $self = { file => iFile->new(shift) }; bless $self, $class; }

Note that the above code does not contain the methods (I excluded them for making my post to be compact).
In the above code, in the package Library, I have the line
($self->{library}) = ($lib->get_list("library"));

If I do not use the paranthesis like above and have instead:
$self->{library} = $lib->get_list("library"); I get an error.

Please excuse if this is a very basic question...but why do I need to use the ($self->{library}) = ($lib->get_list("library")); instead of $self->{library} = $lib->get_list("library"); to make my program work?
--Jessica

Replies are listed 'Best First'.
Re: A new user question on OO Perl
by chromatic (Archbishop) on Dec 19, 2007 at 05:11 UTC

    You probably don't need the parentheses on the right side of the expression; they're superfluous. The parentheses on the left side put the entire expression into list context. The get_list method probably returns a list, which when evaluated in scalar context will give you something besides the first element of the list.

    In list context, you'll get the first element of the list, which is presumably what you expect.

    This is all just a guess though; we'd have to see the code in get_list to tell you for sure.

      Hi, Here is the get_list method in Data::Container package:
      sub get_list { my ($self, @capwords) = @_; my $items = $self->{items}; my @list = (); return @list unless $items; foreach my $item (@{$items}) { push @list, $item if ($item->is(@capwords)); } return @list; }

      The get_list does return an array.
      Thanks for the hint you provided. I understand it now better.
      One final question though. In the package Library, I have the following:
      sub get_list { my ($self, @keywords) = @_; return $self->{lib}->get_list(@keywords); }

      The method Library::get_list will actually return the result of Data::Container::getlist. Is my understanding correct?
      --Jess

        As to your final question, if $self->{lib} contains a Data::Container object, then the method will return the result of the get_list method on that object.

        You could rewrite your get_list method to behave appropriately in scalar context with:

        sub get_list { my ( $self, @capwords ) = @_; # return nothing in void context return unless defined wantarray; # return nothing if there are no items return unless $self->{items}; # return filtered list in list context return grep { $_->is( @capwords ) } @{ $self->{items} } if wantarr +ay; # return the first filtered element in scalar context for my $item ( @{ $self->{items} } ) { return $item if $item->is( @capwords ); } # explicitly return nothing if there's no filtered list return; }

        The important thing to learn from this is wantarray, thought you might also like to know that return with no expression returns an empty list in list context and undef in scalar context.

Re: A new user question on OO Perl
by kyle (Abbot) on Dec 19, 2007 at 05:11 UTC

    I get an error.

    What's the error?

    The example code you've posted is not complete, won't work as posted. I made a few changes to get it to run, and it runs without errors.

    The difference between ($x) = (f()) and $x = f(), in a nutshell, is that the first one puts the function in a list context. In the second example, the function is called in a scalar context.

    Without more of your code, I don't know what's going on.

      Kyle,
      The actual code is very long. I will try to get it condensed and post it.
      --Jessica.