Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

I've never seen this syntax before

by ChuckularOne (Parson)
on Apr 04, 2001 at 23:15 UTC ( #69811=perlquestion: print w/ replies, xml ) Need Help??
ChuckularOne has asked for the wisdom of the Perl Monks concerning the following question:

Help, I don't get it. I'm reverse engineering somebodies broken code. There is a line of code in there that I just don't get! here it is:
return not Execute($cmd);
where Execute() is a stupid little sub that basically does:
$txt = `$cmd 2>&1`; print $txt;
To capture stdout and stderr to a file.

What is the syntax of: return not Execute($cmd); doing?

Many Thanks.
Your Humble Servant,
-Chuck

Comment on I've never seen this syntax before
Select or Download Code
(ar0n) Re: I've never seen this syntax before
by ar0n (Priest) on Apr 04, 2001 at 23:18 UTC
    It returns the inverse of whatever Execute($cmd) returns. If Execute($cmd) returns true, the sub returns false and vice versa.

    [ar0n]

      Thanks, that was what I thought, but there is no return in the Execute() sub, so it looks like it's useless code.

      Your Humble Servant,
      -Chuck
        No necessarily useless... the last line of a subroutine becomes its return value if none is specified explicitly... so: <CODE> sub foo { 1; } #and sub foo2 { return 1; } both return 1
                        - Ant
        In Perl, the value of last evaluated statement is returned in a subroutine - the return token is optional, but you shouldn't abuse this (too much).

        Jeff

        R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
        L-L--L-L--L-L--L-L--L-L--L-L--L-L--
        
      What exactly does `inverse' mean in perl? Is it only in terms of 0 and everything else? Or is there some mathematical inversion that could be harnessed? Like reciprocals etc. I'm hoping it's the latter, but I suspect it's the former...

      --Psi
      print(pack("h*","e4f64702566756e60236c6f637560247f602265696e676021602075627c602861636b65627e2")."\n");

        If the operand is 0, a string beginning with "0", a null string, or undef the result is 1, otherwise the result is a null string.
Re: I've never seen this syntax before
by cLive ;-) (Parson) on Apr 04, 2001 at 23:23 UTC
    not == !

    return !Execute($cmd); == return not Execute($cmd);

    ie

    if (Execute($cmd)) { return 0; } else { return 1; }

    cLive ;-)

      return 0; should be return "";
        It doesn't matter. I can return "0", 0, or "" for false. They are equivalent in a boolean context.

        cLive ;-)

        Actually it is more subtle than even that. You can't replace !1 with 0 nor with "".

        0 is wrong because, in a string context, !1 is "" while 0 is "0".

        "" is wrong because, when warnings are turned on, 0+"" generates q(Argument "" isn't numeric) while 0+!1 is silent.

                - tye (but my friends call me "Tye")
Re: I've never seen this syntax before
by suaveant (Parson) on Apr 04, 2001 at 23:24 UTC
    no... the 2>&1 redirects stderr to stdout, so stderr and stdout from the command goes into $txt. if the sub ends with a print, the print will always return true unless it was unable to print. So the sub will probably almost always return true, the not will make the funtion return false. If it can't print, the return will be true... odd code...
                    - Ant
      I now see what's going on here. There is no || die on the file open. This freaky thing is testing to see if the file opened AFTER trying to write to it. Cool I can rip it out and put a file open || die in there.

      Good thing the guy who wrote this doesn't work here anymore.

      Your Humble Servant,
      -Chuck
Re: I've never seen this syntax before
by dws (Chancellor) on Apr 04, 2001 at 23:30 UTC
    If the intent is to return false if $cmd executes correctly, the code you've shown is badly, badly broken. print is going to return 1 regardless of whether `$cmd 2>&1`; executes correctly, and regardless of whatever ends up in $txt.

    Look downstream of the return, and see if any callers are actually using the return value, and whether they think it has anything to do with the command executing properly. You may get to replace Execute with something that works. Fortunately, that's an easy fix. I'll leave it as a learning exercise.

(boo)Re: I've never seen this syntax before
by boo_radley (Parson) on Apr 04, 2001 at 23:36 UTC
    Remember that perl will implicitly return the sub's last operation's result.
    So, what you're seeing is someone printing something, and then notting the success of the print. Which is, uh, odd, since print usually works unless something uniquely catastrophic has happened. compare
    print print "foo\n";
    and
    print not print "foo\n";
    .

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-11-29 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls