Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

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


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

why not just:

retrun thatroutine() if thatroutine()

edit: or

 if (thatroutine()) {return $_}

--
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^3: looking for a good idiom: return this if this is true
by merlyn (Sage) on Mar 07, 2005 at 15:18 UTC
    retrun thatroutine() if thatroutine()
    That calls thatroutine() twice, which will perform things twice. Not good.
    if (thatroutine()) {return $_}
    $_ isn't getting set there. Are you expecting the variable to be psychic?

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

      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)))'
        $_ 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://437226]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found