Im a regular abuser of AUTOLOAD, but would like to create customized routines for each invocation - essentially munging the method-name so its unique for each caller.

The following creates anonymous subroutines, then (in same statement) gives them a name by stuffing them in the symbol table. But once created, the method short-circuits any further AUTOLOAD invocation; the method is already built.

what I really need is enough optree-optimization fu to change the method-name in the caller code so that it can point to a name-munged method.

what I want to do is partly satisfied by $meth .= $line; but then the munged method, ex: auto_25(), wont be invoked by A::auto() called in main, and Im back to using AUTOLOAD as a dispatcher.

I have tried some lesser ways to achieve this, but no joy; code below attempts to put some context in arg-0, but is defeated by lack of CONSTANT-ness, a new {} is constructed each time, defeating a hash with munged method-names and a-sub refs.

#!/usr/local/bin/perl -w use strict; no strict 'refs'; package A; use Data::Dumper; use vars qw($AUTOLOAD); my %cache; sub AUTOLOAD { (my $meth = $AUTOLOAD) =~ s/.*:://; return if $meth eq 'DESTROY'; my ($package, $filename, $line, $subroutine) = caller(0); print "$meth called by: $package, $filename, $line, $subroutine\n" +; $cache{"$package, $filename, $line, $subroutine"} = $_[0]; $_[0]->{line} = $line; *{'A::'.$meth} = sub { print "invoking $meth from: $package, $filename, $line, $subroutin +e\n"; print "\twith ", Dumper (\@_); }; goto &{'A::'.$meth}; } package main; foreach (1..2) { # if hashref were CONSTANT, and not rebuilt for each invocation, # it could preserve the A::fancy({arb=>1},1); A::fancy({ok=>2},2); A::auto({}); A::auto({}); } use Data::Dumper; print Dumper (\%A::);
if you run this, youll find the line# reported is same for each call (due to closure on $line). I could get new $line for each invocation, but that defeats a main goal of using the a-sub (efficiency; avoiding unneeded caller()s).

In reply to munging function names with AUTOLOAD by jimc

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.