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

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.

Replies are listed 'Best First'.
Re^4: looking for a good idiom: return this if this is true
by wolfger (Deacon) on Mar 07, 2005 at 17:54 UTC
    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.