didess has asked for the wisdom of the Perl Monks concerning the following question:
trying to explain ternary operator in Perl to someone, I found this "strange" (for me) behaviour:
when I use the return value of a sub in the predicate evaluation of a ternary operation,
nothing seems to work when this evaluation happens between parentheses. Some explanation ?
The example : This code :
gives this output :sub get1 { return 1; } print get1() ? "1: Flag is up\n" : "1: Flag is not up\n"; print &get1 ? "2: Flag is up\n" : "2: Flag is not up\n"; print (get1()) ? "3: Flag is up\n" : "3: Flag is not up\n"; print (&get1 ) ? "4: Flag is up\n" : "4: Flag is not up\n";
Where did cases 3 and 4 disappear ?1: Flag is up 2: Flag is up
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: parentheses around a function call in a ternary choice
by moritz (Cardinal) on Jun 23, 2009 at 20:56 UTC | |
by didess (Sexton) on Jun 23, 2009 at 21:11 UTC | |
|
Re: parentheses around a function call in a ternary choice
by FunkyMonk (Bishop) on Jun 23, 2009 at 21:01 UTC | |
by didess (Sexton) on Jun 23, 2009 at 21:20 UTC | |
by FunkyMonk (Bishop) on Jun 23, 2009 at 21:40 UTC | |
|
Re: parentheses around a function call in a ternary choice
by Marshall (Canon) on Jun 23, 2009 at 22:58 UTC |