Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

I don't understand this grep

by gwhite (Friar)
on Nov 05, 2008 at 17:43 UTC ( #721741=perlquestion: print w/replies, xml ) Need Help??
gwhite has asked for the wisdom of the Perl Monks concerning the following question:

I had a consultant do some a work a couple of years ago and am now trying to solve a problem and I can't figure what this is doing: program... $MSG->trace( "my message"); module.. sub trace { shift->_message( TRACE, @_ ) } sub channels_by_fh { map { $_->[0] } @{ shift->{handles} } } sub channels_by_file { () } sub channels_by_email { () } sub channels_by_level { grep { $_[0]->level( $_[1] ) } $_[0]->channels + } sub _message { my $self = shift; my $level = shift; foreach my $channel ( $self->channels_by_level( $level ) ) { print $channel $self->tag($level) . "@_" . "\n"; } }

Right now I am getting an error message of "Can't locate object method "channels" via package..." which is pointed at the line "sub channels_by_level", I am self taught and don't understand what the subroutine is doing, I am not familiar with what grep does or which direction this lines is working. I am hoping someone can explain this for me.


Replies are listed 'Best First'.
Re: I don't understand this grep
by ccn (Vicar) on Nov 05, 2008 at 17:52 UTC

    The following code

    sub channels_by_level { grep { $_[0]->level( $_[1] ) } $_[0]->channels + }

    can be written more clear such as:

    sub channels_by_level { my $self = shift; my $level = shift; return grep { $self->level( $level ) } $self->channels; }

    Method $obj->channels_by_level($level) returns those channels which has level given in an argument.

    The problem is that your object doesn't have channels. To understand why it doesn't more code need to be shown.

      The curious thing is that the test in the grep:

      return grep { $self->level( $level ) } $self->channels;
      contains nothing that depends on whatever $self->channels is or returns. It could equally be written:
      return $self->level($level) ? $self->channels : () ;
      but that doesn't answer the question of why $self->channels appears to be missing.

        I wonder if it's supposed to be
        return grep { $_->level( $level ) } $self->channels;

        Well, that won't work if $self->channels returns a list, which seems likely, based on the name.

        Update: Need to engage eyeballs before fingers. Thanks massa and jwkrahn for an appropriate whack on the side of the head.

        G. Wade
Re: I don't understand this grep
by toolic (Bishop) on Nov 05, 2008 at 18:06 UTC
    I am not familiar with what grep does
    As the documentation says, grep filters a list according to a specified condition, and returns a new list, when called in a list context, as your code does.

    Since your sub does not have an explicit return, the sub returns what grep returns, namely a list.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://721741]
Approved by ccn
[Corion]: Heh - a good talk on Youtube about why you want to keep your Genome as secret as possible (and not use it for vanity stuff like 23andme , or some ancestry stuff)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-01-19 18:11 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (222 votes). Check out past polls.