develooper Front page | perl.perl5.porters | Postings from March 2001

Re: unexpected result of stringification.

Thread Previous | Thread Next
From:
nick
Date:
March 18, 2001 08:16
Subject:
Re: unexpected result of stringification.
Message ID:
E14efn5-0001jT-00@roam1
<abigail@foad.org> writes:
>On Mon, Mar 12, 2001 at 12:30:43PM +0000, David Mitchell wrote:
>> Konovalov, Vadim" <vkonovalov@lucent.com> wrote:
>> 
>> > print 'foo'=~/(.*)/ &&  $1,   'bar'=~/(.*)/ && $1,  "\n";
>> > print 'foo'=~/(.*)/ && "$1", 'bar'=~/(.*)/ && "$1", "\n";

>Yes, for calling functions. And that's documented. But perlop says:
>
>    The `||' and `&&' operators differ from C's in that, rather than
>    returning 0 or 1, they return the last value evaluated.
>
>It clearly states *value*. Not address. Not alias. Value.
>
>And before you say it's because print is being called and that's a function,
>you are wrong. That's irrelevant; it's about what && returns.
>
>    $a = (($foo = 2) && $foo) + (($foo = 3) && $foo);
>    print $a;
>
>prints 6, not 5 as you would expect if && returns a value.

The value in this case is $1 and that variable is then changed.
The 'fail' is just the same as the (expected to me) behaviour of:  

perl -e 'print STDOUT ($a = 4),($a = 5);'   

-- 
Nick Ing-Simmons


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About