When I run the line outputted from the Deparse inplace of his original code
I get errors! What's up with that?
#!/usr/bin/perl -w
use strict;
my $num=3;
my $nextnum;
((($num == 3) ? ($nextnum = 4) : $nextnum) = "unknown");
print $nextnum;
----- Original Message -----
From: "Paul Johnson" <[EMAIL PROTECTED]>
To: "David Rankin" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Monday, August 13, 2001 4:09 PM
Subject: Re: Tertiary Operator Question
> On Mon, Aug 13, 2001 at 05:50:14PM -0400, David Rankin wrote:
>
> > #!/usr/bin/perl -w
> > use strict;
> > my $num=3;
> > my $nextnum;
> > $num==3 ? $nextnum=4 : $nextnum="unknown" ;
> > print $nextnum;
> >
> > It prints "unknown". I'd expect it to print "4" because $num==3 would
> > evaluate to true.
>
> You're being hit by precedence.
>
> $ perl -MO=Deparse,-p -e '$num==3 ? $nextnum=4 : $nextnum="unknown"'
> ((($num == 3) ? ($nextnum = 4) : $nextnum) = 'unknown');
>
> But if you are assigning to the same variable in both branches, you're
> better off writing like this:
>
> $nextnum = $num == 3 ? 4 : "unknown";
>
> $ perl -MO=Deparse,-p -e '$nextnum = $num == 3 ? 4 : "unknown"'
> ($nextnum = (($num == 3) ? 4 : 'unknown'));
>
> --
> Paul Johnson - [EMAIL PROTECTED]
> http://www.pjcj.net
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]