in reply to Re: 'my' headache...why doesn't this work?
in thread 'my' headache...why doesn't this work?
And as should always be kept in mind when using B::Deparse, the code output can be and in this case is clearly wrong.
Obviously if it was correct the my clause would occur before the for.
Ok, well apparently this isnt so obvious so here it is again. Statement Modifiers do not scope the statement that they modify. For instance the following are not the same because the scope they execute in is different.
So when B::Deparse takes the code of the first example and naively converts it to the second it has produced incorrect output. This can be seen by the followingprint "In main scope $_\n" for 1..10; for (1..10) { print "In scoped block:$_\n"; }
So obviously as the input code compiles under strict but the output wont, the two cannot be the same.D:\Development>copy con deparse.pl use strict; my $x=$_ foreach 1..10; print $x; ^Z 1 file(s) copied. D:\Development>copy con deparse.out.pl use strict; ^Z 1 file(s) copied. D:\Development>perl -MO=Deparse deparse.pl >>deparse.out.pl deparse.pl syntax OK D:\Development>perl deparse.pl D:\Development>perl deparse.out.pl Global symbol "$x" requires explicit package name at deparse.out.pl li +ne 5. Execution of deparse.out.pl aborted due to compilation errors. D:\Development>
Its interesting actually that B::Deparse handles conditional and while loop modifiers correctly, but not for loop modifiers.
Incidentally we can reduce this bug down to the following very simple exampleD:\Development>perl -MO=Deparse -e "use strict; my $x=1; my $y=1 if $x +; print $x.' '.$y;" my $x = 1; my $y = 1 if $x; print $x . ' ' . $y; -e syntax OK D:\Development>perl -MO=Deparse -e "use strict; my $x=10; my $y=$x whi +le ($x--); print $x.' '.$y" my $x = 10; my $y = $_ while $x--; print $x . ' ' . $y; -e syntax OK
This clearly shows what is going wrong here (note the output wont compile under strict) and it clearly shows that at least with regard to for modifiers B::Deparse produces incorrect output. (There are other examples listed in the B::Deparse documentation.)D:\Development>perl -MO=Deparse -e "use strict; my $x=1 for 1; print $ +x" foreach $_ (1) { my $x = 1; } print $x; -e syntax OK D:\Development>perl -e "use strict; my $x=1 for 1; print $x"
Always be careful when using B::Deparse to make comments about what Perl is doing. B::Deparse isnt perl and only makes a good (well, very good) attempt at the translation.
Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.
|
|---|