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

Re: unexpected result of stringification.

Thread Previous | Thread Next
Nick Ing-Simmons
March 12, 2001 03:59
Re: unexpected result of stringification.
Message ID:
Vadim Konovalov <> writes:
>print 'foo'=~/(.*)/ &&  $1,   'bar'=~/(.*)/ && $1,  "\n";
>print 'foo'=~/(.*)/ && "$1", 'bar'=~/(.*)/ && "$1", "\n";
>results in:
>which was unexpected to me.

It took me a little while to explain it as well.

>While there may be found a way to explain such behaviour, it may result in
>errors that are quite hard to find.

The explanation of course is that both args to the print are evaluated
before print itself is called. And in the non-stringified case 
both args are the (single) $1 variable. So it prints its value 
at time print is called twice. In the stringify case a copy is made which 
saves the value of $1 and time arg was evaluated.

>Is this a bug or not?

It cannot be - changing either is bound to break something.

>Is there a way to fix this?

Not without breaking scripts which rely on the behaviour.

>Tried 5.6.0 and 5.005_02 on Win32 and Unix systems.
>Best wishes,
><!ENTITY Vadim REALLIFE "Vadim V.Konovalov, St.Petersburg, Russia">
Nick Ing-Simmons <>
Via, but not speaking for: Texas Instruments Ltd.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About