It's not "queueing" the END block at run time. The problem is that you're dieing before assigning a value to your variable. So, in effect, you're trying to unlink undef.

You're thinking that the END block shouldn't be called if the subroutine is not called because you've placed the definition inside the subroutine body... That's incorrect. Named subroutines (of which BEGIN and END are just special breeds) are non-lexical. That means they all belong to the package they're declared in, not just the block. The only thing you're accomplishing by declaring your END routine inside the body of new_tmpfile_name is that you're creating a named closure (ie, END keeps a reference to $name, thus keeping it alive longer than it otherwise would be).

A corollary to this is the fact that if you were to call new_tmpfile_name multiple times, END would still only be called once and only the first temp file unlinked. I suspect this is not what you mean to happen.

A possible solution to do what you seem to be attempting would be to do a string eval. This has an unfortunate side-effect of being a little slow. :-( But it would definately work. :-) Anyway, I hope this bit of explaination helps. Have fun coding!

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.


In reply to (bbfu) Re: END blocks created at run time? by bbfu
in thread END blocks created at run time? by lemmett

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.