In Spreadsheet::Read Win32::LongPath::openL, I wrote something like:

my $tmp = File::Temp-> new( SUFFIX => '.ods' )-> filename; copyL 'whatever.ods', $tmp; say 'ok' if defined ReadData( $tmp );

which I later realized didn't work exactly as intended. Temp file (and object) no longer existed after 1st statement, copyL simply "hijacked" file name, and temp file wasn't cleaned-up at program end.

Initially I wanted it shorter as

copyL 'whatever.ods', my $tmp = File::Temp-> new( SUFFIX => '.ods' )-> + filename; say 'ok' if defined ReadData( $tmp );

it wasn't "ok" for reason stated above. Instead, in retrospect, this would work:

copyL 'whatever.ods', my $tmp = File::Temp-> new( SUFFIX => '.ods' ); say 'ok' if defined ReadData( $tmp-> filename );

Then object stringifies itself in 1st statement, and survives because bound to a variable. From practical point, that's about it all. However, during investigation I was puzzled by unexpected (though maybe not practically useful) behaviour:

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 'global' if ${^GLOBAL_PHASE} eq 'DESTRUCT'; $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;

which says "3". All copying completes successfully, and I'd expect objects destroyed and temp files deleted by end of each of 4 statements. But, somehow, object survives if bound to lexical variable in "do" block, but not in case of subroutine call. Why?

Auxiliary question: object was not destroyed during global destruction at program end, but when?


In reply to 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.