http://www.perlmonks.org?node_id=170930


in reply to @_ still mystifies me

A great way of figuring how Perl parses a particular piece of code is to use the B::Deparse module. That will attempt (usually successfully) to deparse your code and make its meaning more clear. To run it against a program, you would do something like this:

perl -MO=Deparse someprog.pl

The output will be Deparse's interpretation of what the code really does. Usually, the result is not too far off of what you have. In this case, I ran Deparse against you snippet and the result surprised me. Not in what it was saying to do, but in how it said it. In any event, it was much more clear. This should help you in the future -- I hope:

$ perl -MO=Deparse -e 'my @lexlist = @_ or sort keys %lexicon' sort keys %lexicon unless my(@lexlist) = @_;

So, as you can see, that parsed completely differently from your expectations. Hope this helps.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Re: @_ still mystifies me
by danger (Priest) on Jun 01, 2002 at 17:49 UTC

    I'm glad Ovid (++) mentioned the Deparse module, but be aware that different options can affect the output:

    # no options: $ perl -MO=Deparse -e 'my @lexlist = @_ or sort keys %lexicon' sort keys %lexicon unless my(@lexlist) = @_; #--- # using -x7 (level 7 expands code into equivelant logical constructs # using &&, ?:, and do{}) -- (giving us back nearly the original) $ perl -MO=Deparse,-x7 -e 'my @lexlist = @_ or sort keys %lexicon' my(@lexlist) = @_ or sort keys %lexicon; #--- # however, using -p (to fully parenthesize) can often help expose # such problems as well: $ perl -MO=Deparse,-p -e 'my @lexlist = @_ or sort keys %lexicon' ((my(@lexlist) = @_) or sort(keys(%lexicon)));

    It is sometimes useful to explore the output of Deparse with various options applied.