Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

I don't understand this grep

by gwhite (Friar)
on Nov 05, 2008 at 17:43 UTC ( [id://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

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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-25 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found