#!/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 (d +ont 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 t +o 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__

In reply to AUTOMUNGE revisited 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.