Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Perl parser tortured (was: Perl Idioms Explained)

by dada (Chaplain)
on Mar 02, 2004 at 16:12 UTC ( #333293=note: print w/ replies, xml ) Need Help??


in reply to Perl Idioms Explained - my ($foo, $bar) = @{shift(@_)}{qw/ -foo -bar /}

interestingly enough, I used something (partly, at least) similar in a recent post of mine.

in my endless quest for Evil Things To Do, I was looking for a way to express this:

sub deref { my $ref = shift; return $$ref; }
in a more succint way. the first thing, of course, was to get rid of $ref, and so I wrote:
sub deref { ${$_[0]} }
but this is nothing sexy. so I thought, maybe I can get rid of $ref while still retaining the shift:
sub deref { ${shift} }
but this doesn't work, and it took me a bit (and the help of B::Deparse) to understand why.

${shift} is interpreted by the parser as a way to write $shift, and of course it returns nothing.

so I realized that the Perl parser needs to be hinted to get shift as a keyword, and this can be done in (at least :-) 3 ways:

sub deref { ${shift@_} } # too much explicit sub deref { ${+shift} } # supersticious? cfr. Roger sub deref { ${shift;} }
the last one I find particularly surprising :-)

cheers,
Aldo

King of Laziness, Wizard of Impatience, Lord of Hubris


Comment on Re: Perl parser tortured (was: Perl Idioms Explained)
Select or Download Code
Re: Re: Perl parser tortured (was: Perl Idioms Explained)
by TimToady (Parson) on Mar 02, 2004 at 21:41 UTC
    That will be fixed in Perl 6, which will distinguish
    ${shift} # always an expression in the middle
    from
    $«shift» # always a literal
    It's really quite amazing how much just adding one set of brackets to a language helps.

      It's really quite amazing how much just adding one set of brackets to a language helps.

      Yeah, but its just really annoying how long it takes the keyboard manufacturers to actually put the new brackets somewhere useful.

      IMO this is something you need to consider, and especially fromt he POV that keyboards vary quite a bit around the world. AltGr-7, AltGr-8, AltGr-9, Algr-10 are where the { [ ] } brackets live in germany which is already frustrating enough for someone that grew up in Canada. I dread having to type these new brackets. But no doubt you are already hip to this problem :-)


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-09-02 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (21 votes), past polls