Try writing your loop like this:
for(my $i=0;$i<50;$i++) { my $tmp = new File::Temp('DIR' => '/tmp', 'UNLINK' => 1); $SIG{INT} = sub { unlink "/tmp/".$tmp->filename; die }; ## <- add t +his line sleep(1); }
Note that it's important to include the "die" call in the sub that gets assigned to $SIG{INT} -- otherwise, the script won't actually exit on ctrl-C.

(Update -- to explain: when you interrupt the script, this will generally be during the sleep call, and unless you declare a signal handler to catch that, the File::Temp module will never get the chance to do its normal clean-up.)

Another update -- check the perlipc docs, and your own system's manual regarding signals; you'll probably want to trap other things in addition to SIGINT (e.g. SIGTERM, which is what the shell "kill" command sends by default).


In reply to Re: File::Temp.. What am I missing? by graff
in thread File::Temp.. What am I missing? by devnul

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.