On 29 March 2010 14:02, Jeff Soules <sou...@gmail.com> wrote:
> Hi all,

Hiya,


> Am I missing something?  I have the following chunks of code:
>
> EX 1:
>    if ($foo == 1){
>        $bar = 0;
>    }else{
>        $bar = 1;
>    }
>
> EX 2:
>    ($foo == 1) ?
>        $bar = 0 :
>        $bar = 1;
>
> These are logically equivalent, right?  But when I've run the latter,
> $bar always comes away with a value of 0, regardless of the value of
> $foo.
>
> This is particularly confusing, as the following snippet:
> EX 3:
>    ($foo == 1) ?
>        warn "Got 0\n" :
>        warn "Got 1\n";
>
> is producing the results I expect.
>
> Am I doing something stupid or missing something obvious?

There must be something else going on within your code.

perl -e '$foo=1; printf("%d\n", ($foo==1)? 1 : 0);'
1
perl -e '$foo=0; printf("%d\n", ($foo==1)? 1 : 0);'
0

I'm puzzled by the list context Jeff has mentioned. I use ($foo == 1)
in ternary expressions and always get the expected results. I may have
to go and re-visit some code to find out what I've done.
Good luck,
Dp.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to