http://www.perlmonks.org?node_id=416807

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

On my machine (Fedora 2.6.7, perl 5.8.5) the following one liner prints true:

 perl -e 'if (undef() eq undef()) {print "true" }else{ print "false"}'

This variation (unexpectedly) prints nothing:

 perl -ew 'if (undef() eq undef()) {print "true" }else{ print "false"}'

Do any of you brothers (be you either male or female) have a clue as to why this difference in behavior?

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Replies are listed 'Best First'.
Re: Unexpected effect of -w with undef
by Aristotle (Chancellor) on Dec 22, 2004 at 16:25 UTC

    You are giving Perl the code w to execute and the filename if (undef() eq undef()) {print "true" }else{ print "false"} as an argument to it. You probably wanted to say -we… Most of perl's switches are order sensitive.

    strict catches this, FWIW:

    $ perl -Mstrict -ew 'if(undef() eq undef()){print "true"}else{print "f +alse"}' Bareword "w" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors. $ perl -Mstrict -we 'if(undef() eq undef()){print "true"}else{print "f +alse"}' Use of uninitialized value in string eq at -e line 1. Use of uninitialized value in string eq at -e line 1. true

    Makeshifts last the longest.

      Aw.... Dang!

      /me smacks forehead.

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

        Hehehehe. I looked at it in befuddlement for a moment as well after I pasted the code in a terminal and it didn't print a thing. Then I went “Oh! Heh, he'll be really embarrassed when he reads the answer.” Don't sweat it though, we all have our moments. :-)

        Makeshifts last the longest.

Re: Unexpected effect of -w with undef
by bgreenlee (Friar) on Dec 22, 2004 at 16:30 UTC

    Heh...if you used -w, you would have caught this.

    $ perl -wew 'if (undef() eq undef()) {print "true" }else{ print "false +"}' Unquoted string "w" may clash with future reserved word at -e line 1. Useless use of a constant in void context at -e line 1.

    Sorry...couldn't resist.

    -b

Re: Unexpected effect of -w with undef
by Stevie-O (Friar) on Dec 22, 2004 at 16:29 UTC
    Ahh, a case of syntactic sugar deceiving the user.

    You're probably thinking that the '-w' is supposed to cause warnings to be reported. And normally, it is.

    However, the the first thing following -e is the script. And that's not the mini-script you wrote: it's the string 'w'. Perl treats that as a bareword and thinks you wrote a script that consists solely of the string 'w', which in a void context doesn't do anything. The 'if (undef() eq undef())...' is the first thing in @ARGV.

    Change your perl -ew to perl -we and you might get something more like what you expected.

    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
Re: Unexpected effect of -w with undef
by gaal (Parson) on Dec 22, 2004 at 17:10 UTC
    Yes, this is called the "ew!" gotcha.

    See also: the pie gotcha.