#!/usr/local/bin/perl -w =for request for assistance im trying to work out an auto-cat feature for Log::Log4perl, which needs greater efficiency of caller() (or 1 time use of it;) =head SYNOPSIS use Log::Log4perl::AutoCategorize ( alias_as => 'Logger' ); # want this Logger->debug() # to mean something like Logger->debug_<$callerpkg,$caller_sub,$caller_ln>(@_) =head1 A has autoload that munges a name, and installs a hello-from method into the symbol table. user program must create the name before invoking it, so that =cut use strict; no strict 'refs'; package A; use Data::Dumper; use vars qw($AUTOLOAD); use Carp 'cluck'; sub AUTOLOAD { # compile and goto& munged method-name # (my $meth = $AUTOLOAD) =~ s/.*:://; return if $meth eq 'DESTROY'; my ($package, $filename, $line, $subroutine) = caller(0); my $buf = "<$meth>: $package, $filename, $line, $subroutine\n"; $buf =~ s|[\./]||g; print "creating: $buf"; *{'A::'.$meth} = sub { print "invoked: $buf"; # force string interpolation, no closure (dont need) # print "\twith ", Dumper (\@_); }; goto &{'A::'.$meth}; } package main; A::auto("string{}"); cooperative_dynamic_name_invoke(); print "done\n"; sub cooperative_dynamic_name_invoke { # macro-like invoke of auto-names # invoker help needed; by creating custom sub-name # for each lexical invocation foreach (1..2) { # autoload compiles sub on 1st iteration # (and vivifies name in symbol table) # 2nd time, new asub called directly my $munge = "dyn_name_000"; # wo reinit, would just make 3,4 $munge++; # this doesnt give string++, but irrelevant to test A->$munge("1st try"); $munge++; A->$munge("2nd try\n"); $munge++; A->$munge("3rd try\n"); } } __END__ =head1 for HUNCH munging name (for me, here) has 2 steps: 1. producing munged name from user context. (he called me from X, he knows why) includes - by def - adding that function to symbol table 2. causing that new name to be called from that spot in code (job for optimize ??) I dont understand; 1. how to recognize (particularly at compile time) whether AUTOLOAD would eventually be called, or how to get the lexical scope during compilation. a compile-time analogue to caller() would be cool. 2. when to 'require optimizer extend ....' whats it mean to do so in INIT{} or CHECK {} blocks ? My hope is that porters with optimizer.pm-fu will find this to be a killer-app with sufficient merit for suggestions or solutions ;-) (clever use of) would address is how to patch the opcode that invokes the referent-function. =cut use optimizer extend => sub { print "dump ", Dumper \@_ if 0 #or $_[0]->name() eq "goto" #or ref $_[0] =~ /CV/i ; # =~ /__ANON__/; }; sub loop { 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::); } __END__