I'm dynamically generating subroutines and assigning them to actual subs:

my @types = qw( date integer text timestamp ); foreach my $type (@types) { my $function = uc $type; no strict 'refs'; *$function = sub { if ( looks_like_number( $_[0] ) ) { die "Datatype '$type' does not require a numeric length '$ +_[0]'"; } return { type => $type, @_ }; }; }

However, sometimes I need to alias those subroutines:

*TINYTEXT = \&TEXT;

That cause a problem because the '$type' referred to in the error message should actually be the lower-cased name of the subroutine, not the original type that was generated. Both 'TINYTEXT(3)' and 'TEXT(3)' will generate an error message referring to 'text'.

I tried to use caller, but that doesn't work because it reports 'main::__ANON__' instead of which CODE slot in the typeglob that this anonymous sub is assigned to. Is there any way I can figure that out from within an anonymous sub?

(I can get around this in numerous ways by providing 'sub' to 'type' mappings or manually coding the aliased subroutines, but I'd much prefer to avoid the grunt work).

Cheers,
Ovid

New address of my CGI Course.


In reply to What CODE typeglob slot is my anonymous sub in? by Ovid

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.