Somehow the lexical variable in the do block gets attached to the upper scope. As shown with the test below:
This works however:use 5.014; use warnings; package Temp; use parent 'File::Temp'; sub new { my $self = shift; $self-> SUPER::new( DIR => '.' ) } sub DESTROY { my $self = shift; say "Destruction during ", ${^GLOBAL_PHASE}; $self-> SUPER::DESTROY } package main; use File::Copy; sub foo { my $h = Temp-> new } die unless -f 'x'; # this file must exist { copy 'x', my $fn1 = Temp-> new-> filename or die; copy 'x', my $fn2 = do { Temp-> new }-> filename or die; copy 'x', my $fn3 = do { my $h = Temp-> new }-> filename or die; copy 'x', my $fn4 = foo-> filename or die; say 1 if -f $fn1; say 2 if -f $fn2; say 3 if -f $fn3; say 4 if -f $fn4; say "End of scope"; } say "End of file"; __END__ Destruction during RUN Destruction during RUN Destruction during RUN 3 End of scope Destruction during RUN End of file
because the extra block creates a lexical scope inside the do block.copy 'x', my $fn3 = do { { my $h = Temp-> new } }-> filename or die;
The documentation for do states that it does not work as a loop, when all other blocks (even bare one, without the for or while keyword) do, (which means next and last and redo don't work on do blocks). So somehow do blocks aren't blocks either? What's weird is that there is a "local" scope bound to do, so
does print; "Hi" as expected...$_ = "Hi"; do { local $_ = "Hello" }; print
In reply to Re: File::Temp survival and scope created by "do"
by Eily
in thread File::Temp survival and scope created by "do"
by vr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |