Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Usage patterns strike back

by kappa (Chaplain)
on Mar 18, 2004 at 15:27 UTC ( [id://337698]=obfuscated: print w/replies, xml ) Need Help??

Not that evil as most of the posts here. Ok, not evil at all. But my colleagues are regularly falling into this trap so I thought it could be of some interest.
while(<DATA>){last};while(-<DATA>){print} __DATA__ Just another Perl hacker This gotcha bites too many...

Replies are listed 'Best First'.
Re: Usage patterns strike back
by chip (Curate) on Mar 19, 2004 at 17:56 UTC
    Interestingly, unary minus could default to string or numeric behavior and the trap would still bite.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Usage patterns strike back
by halley (Prior) on Mar 19, 2004 at 18:09 UTC
    The difference is the magical parsing of while (<HANDLE>). Try with B::Deparse:
    while (defined($_ = <DATA>)) { last; }
    -but-
    while (-<DATA>) { print $_; }
    The second form doesn't actually assign the line into any variable.

    (Of course, a negative "Just" is still not negative.)

    --
    [ e d @ h a l l e y . c c ]

Re: Usage patterns strike back
by theguvnor (Chaplain) on Mar 18, 2004 at 21:38 UTC

    Explanation please? I'd like to avoid the trap but I'm not savvy enough to quite take your meaning.

    [Jon]

      You can run it. Then remove that '-' and run again. Compare results. Hm, work it out for yourself, you'll get much more satisfaction, really :)
Re: Usage patterns strike back
by muba (Priest) on Mar 20, 2004 at 10:30 UTC
    Ok, I do understand what's happening, but I don't understand why somebody would want to read data from a HANDLE and then get a negative value of it? My first thought was that -<HANDLE> would maybe read from the last line of the file towards the first line, but that does not seem to be the case. So, what's the trap? Who wants to do this? And what is their target?
      Oh, that's just a short and 'obfu' way of turning <HANDLE> into an expression involving <HANDLE> for the sake of this very example.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-18 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found