Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: I don't understand this grep

by ccn (Vicar)
on Nov 05, 2008 at 17:52 UTC ( [id://721743]=note: print w/replies, xml ) Need Help??


in reply to I don't understand this grep

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.

Replies are listed 'Best First'.
Re^2: I don't understand this grep
by gone2015 (Deacon) on Nov 05, 2008 at 18:07 UTC

    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;
        Yes it's easy to mess $_ and $_[0]. Nobody see that until an error occured.

        I tried adjusting the syntax and got the exact same message. $self->channels is not defined anywhere in the module, so it is either misnamed or missing. I will have to figure that out

        g_White

      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

        That should work fine.   What makes you think that it won't work?

        ??!!!
        Yes it will work with channels returning a list, according to Conditional operator:
        Scalar or list context propagates downward into the 2nd or 3rd argument, whichever is selected.
        And it won't execute $self->level($level) once for each item of the returned list, which is good.

        Now, the reason why channels is missing is because... well, gwhite didn't show any sub channels { ... } in the module... But he didn't state that it does not exist, either...

        []s, HTH, Massa (κς,πμ,πλ)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://721743]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2025-12-15 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (95 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.