abstracts has asked for the wisdom of the Perl Monks concerning the following question:

Hello

Here is something I stumbled across:

print "hello" if 1; # prints hello print "hello" if 0; # doesn't print anything print "hello" if "1"; # prints hello again print "hello" if "0"; # Useless use of a constant in void context at ./test.pl line 3.
This error occurs only if the program is in a file (ie perl -e 'print "hello" if "0"' works as advertized). Could anybody enlighten me for the reason for this seemingly strange behavior?

PS: ("0" and print "hello") fails for the exact reason since perl translates (print "hello" if "0") to ("0" and print "hello") at compile time if I remember the perl parser correctly.

Replies are listed 'Best First'.
(crazyinsomniac) Re: if "0"
by crazyinsomniac (Prior) on Apr 22, 2002 at 09:19 UTC
Re: if "0"
by blakem (Monsignor) on Apr 22, 2002 at 08:49 UTC
      perl -Mdiagnostics -e 'print "hello" if "0"' also give some extra hints, although nothing specific. I am lead to believe that perl perhaps fails to understand what you want with that statement - and that makes some sense, as it is really a strange thing to ask perl to do. ;-) "What do you mean, if this false string is true??" *grin*

      Deparse on my machine also returned a real funny result:

      '???'; -e syntax OK
      Yes I know, Deparse is just a qualified guess. Still funny. :)


      You have moved into a dark place.
      It is pitch black. You are likely to be eaten by a grue.