Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: looking for a good idiom: return this if this is true

by wolfger (Deacon)
on Mar 07, 2005 at 17:54 UTC ( [id://437273]=note: print w/replies, xml ) Need Help??


in reply to Re^3: looking for a good idiom: return this if this is true
in thread looking for a good idiom: return this if this is true

if (thatroutine()) {return $_}
$_ isn't getting set there. Are you expecting the variable to be psychic?

No, I'm not expecting that. I tested this with a one-liner prior to posting, I swear. Problem is, I just tried re-entering that one-liner prior to posting my proof here and.... It doesn't work. I suppose I must have screwed that up somehow, the first time around. I'm not entirely clear on how $_ works.


--
Linux, sci-fi, and Nat Torkington, all at Penguicon 3.0
perl -e 'print(map(chr,(0x4a,0x41,0x50,0x48,0xa)))'

Replies are listed 'Best First'.
Re^5: looking for a good idiom: return this if this is true
by merlyn (Sage) on Mar 07, 2005 at 19:25 UTC
    $_ is set by very few things:
    • An explicit assignment ($_ = 35).
    • The implicit setting for a while() loop reading from a filehandle:
      while (<HANDLE>) { ... } # same as ... while (defined($_ = <HANDLE>)) { ... }
    • The implicit variable of a foreach loop:
      foreach (@list) { ... } # same as foreach $_ (@list) { ... }
    • The implicit variable of a grep/map block (added after prodding, sorry):
      $count = grep { BLOCK } LIST; @result = grep { BLOCK } LIST; @result = map { BLOCK } LIST; map { BLOCK } LIST; # invalidates your warranty
    I think that's it. I don't recall anything else that can set the value. Lots of operations can alter the value, like chomp or s///.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found