http://qs1969.pair.com?node_id=240296


in reply to Re: Re: Simplifying code (Not obfuscation)
in thread Simplifying code (Not obfuscation)

Your test is mixing two different kinds of conditional, the 'if' and the 'or' types.

If/Unless version:

unless ($var = `system thingy`) { die "Failed to get return from call to system\n"; }

Or version:

$var = `system thingy` or die "Failed to get return" from call to syst +em";
Note the use of 'or' instead of ||. The only difference between them is their precendence, but as a result the 'or' doesn't need the braces round the assignment.

Overall good coding style. Nice and readable. If all the code I had to do maintenance on was so readable I'd be a very happy bunny.

Update: Changed the if to an unless since I totally messed up the logic. Oops. Thanks 2mths for pointing that out, shall try and engage brain earlier next time.

Update: Fixed the missing ` problem. Thanks parv. Also updated brain to test all Perlmonks submissions to avoid silly mistakes like this in future.

Replies are listed 'Best First'.
Re: Re: Re: Re: Simplifying code (Not obfuscation)
by 2mths (Beadle) on Mar 04, 2003 at 12:13 UTC
    Again, a very welcome and highly appreciated post, highlighting to me the existance of and difference between "if" and "or".

    I won't pretend to understand the meaning of precedence in this context but I don't think that stops me understanding the pertinent points of your post.

    That said, looking closely at the if solution I would have thought that would die if the system call actually succeeded. Am I missing something?

      No, you're quite right. I was being stupid and messed up, will correct that in a second so as not to confuse others.

      The precedence issue is moderately easy to explain in this context. There's a standard Perl idiom

      open FILE, "file.txt" or die "Error: $!\n".

      This is the equivilent to

      open (FILE, "file.txt) or die "Error: $!\n"

      as the 'or' is evaluated after the open command.

      If you were to try

      open FILE, "file.txt" || die "Error: $!\n"

      you may be surprised to know you'll never get the error message, this is because the || operator is evaluated before the open and so this is actually equivilent to

      open FILE, ("file.txt" || die "Error: $!\n")

      and so would only die if the string literal "file.txt" was blank. Not good. You get a similar thing with thing happening quite often.

      Moral of this story: Generally use || for defaulting values ($a = $b || $c), but use 'or' for logic control similar to the above.

      Oh yeah, and make sure your code dies when it gets an error, not when it doesn't.. I seem to have failed on this one

        Thanks for taking the time to explain that and for doing it so well.

        My future coding should be much improved because I can now consider such issues as this and how to test for errors. At the moment it's all a mish mash of other people's code, cut and pasted. It will be good to be able to write these things from scratch myself.

        I have copied your post into my 'reference' notes. So I shouldn't ever need to ask the question again.
Re: Re: Re: Re: Simplifying code (Not obfuscation)
by parv (Parson) on Mar 04, 2003 at 22:02 UTC
    Molt, the unless version will not work due to missing "`" in the condition.