The goto is not needed, you can just return $val.

To answer your question about your later code sample, since you are using variables declared with my, Perl will correctly capture the current value in the anonymous subroutine. This is called a closure.

To address an issue commented on in your code comment, Re: what is the difference between *a and *a{GLOB}? explains why &$subname is the same as &{*$subname{CODE}}.

Going back to closures, a classic book to learn programming techniques using them is MJD's book Higher Order Perl. Or see Why I like functional programming (long) and Re (tilly) 1 (perl): What Happened...(perils of porting from c) (short) for a couple of examples I wrote that you can puzzle through. I'm a fan of techniques that use closures, but only if I am working with people who understand them. There is a definite learning curve in getting used to them.

Closures can be used in most languages which support lexical scope and anonymous subroutines. That list includes most current dialects of Lisp (the one in emacs is the major exception), most scripting languages (including Perl, JavaScript, Ruby and Python though Python makes it harder than it should be), and virtually any language that calls itself a functional language. Closures are now making their way into mainstream languages. (They are in C#, and are a likely future addition to Java.)


In reply to Re^9: Autoloading tie routines by tilly
in thread Autoloading tie routines by cmac

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.