Update
crazyinsomniac suggested that I expand on my comments here so this node has been changed from its original. (Oh and I retitled it too.)

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.

print "In main scope $_\n" for 1..10; for (1..10) { print "In scoped block:$_\n"; }
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 following
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>
So obviously as the input code compiles under strict but the output wont, the two cannot be the same.

Its interesting actually that B::Deparse handles conditional and while loop modifiers correctly, but not for loop modifiers.

D:\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
Incidentally we can reduce this bug down to the following very simple example
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"
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.)

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.


In reply to Re: Re: 'my' headache...Why Deparse is Wrong. by demerphq
in thread 'my' headache...why doesn't this work? by S_Shrum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.