Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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:

...in program... $MSG->trace( "my message"); ...in 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.

g_White

Comment on I don't understand this grep
Download Code
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.

        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
        I wonder if it's supposed to be
        return grep { $_->level( $level ) } $self->channels;
Re: I don't understand this grep
by toolic (Chancellor) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-09-18 11:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (111 votes), past polls