Interesting, thanks for looking into it further!

I'm not sure what you mean about being able to return a value

I mean that in e.g. my $x = do { my $y = "foo" }, $x is assigned the return value of the assignment ("foo") - you can't do things like for example my $x = do { if (/x/) { "X" } else { "_" } } with a bare { BLOCK }.

This looks like an optimization issue, when do contains only a single statement, I guess it is optimized as a single statement executed in the containing scope

You may be onto something, there are some differences in the following two, although I don't know enough about the internals to say if the differences are relevant to the issue. I do think do { EXPR } shouldn't be optimized to just EXPR...

$ perl -MO=Concise -e 'sub DESTROY{print "Foo"} do{1;my $x=bless{}}; p +rint "Bar"' f <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 3 -e:1) v:{ ->3 - <1> null vK*/1 ->b a <@> leave vKP ->b 3 <0> enter v ->4 - <;> ex-nextstate(main 4 -e:1) v ->- - <0> ex-const v ->4 4 <;> nextstate(main 4 -e:1) v ->5 9 <2> sassign vKS/2 ->a 7 <@> bless sK/1 ->8 - <0> ex-pushmark s ->5 6 <@> anonhash sK* ->7 5 <0> pushmark s ->6 8 <0> padsv[$x:4,5] sRM*/LVINTRO ->9 b <;> nextstate(main 6 -e:1) v:{ ->c e <@> print vK ->f c <0> pushmark s ->d d <$> const(PV "Bar") s ->e $ perl -MO=Concise -e 'sub DESTROY{print "Foo"} do{ my $x=bless{}}; p +rint "Bar"' c <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 3 -e:1) v:{ ->3 - <1> null vK*/1 ->8 - <@> scope vK ->- - <;> ex-nextstate(main 4 -e:1) v ->- 7 <2> sassign vKS/2 ->8 5 <@> bless sK/1 ->6 - <0> ex-pushmark s ->3 4 <@> anonhash sK* ->5 3 <0> pushmark s ->4 6 <0> padsv[$x:4,5] sRM*/LVINTRO ->7 8 <;> nextstate(main 6 -e:1) v:{ ->9 b <@> print vK ->c 9 <0> pushmark s ->a a <$> const(PV "Bar") s ->b
The fact that it works properly when the do contains more than one statement means there isn't a real problem though

It's still pretty inconsistent behavior IMO. Bug #133720 reported.

Update: Minor edits to wording.


In reply to Re^4: File::Temp survival and scope created by "do" by haukex
in thread File::Temp survival and scope created by "do" by vr

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.