Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Attributes are not for the faint of heart

Ain't that the truth! Thank you so much for the suggestion, but it didn't work within MODIFY_CODE_ATTRIBUTES. Sigh... Your "another major catch" seems to be right on. Your proposal exhibits the same behavior as Devel::Peek::Dump - works great after compilation (even in a BEGIN block) but not in MODIFY_CODE_ATTRIBUTES. The association between symbol and name does not appear to be set up until after the MODIFY_CODE_ATTRIBUTES function completes. This small script...

use strict; use warnings; use Devel::Peek(); BEGIN { #Many thanks to Thilosophy #see http://www.perlmonks.org/?node_id=743987 for original source #on which this is based sub getOriginalCodeRefName { my ($pkg, $ref) = @_; no strict 'refs'; while (my ($key,$val) = each(%{*{"$pkg\::"}})) { local(*ENTRY) = $val; if (defined $val && defined *ENTRY{CODE}) { next unless *ENTRY{CODE} eq $ref; # rewind "each" my $a = scalar keys %{*{"$pkg\::"}}; return $key; } } warn "Warning: could not find name for $ref"; return undef; } sub MODIFY_CODE_ATTRIBUTES { my $sPackage = shift @_; my $crSub = shift @_; print STDERR "Dumping $crSub in MODIFY_CODE_ATTRIBUTES:\n"; #Devel::Peek::Dump($crSub); my $sName = getOriginalCodeRefName($sPackage, $crSub); $sName = 'undef' unless defined($sName); print STDERR "Name via Thilosophy method: <$sName>\n"; return (); } } BEGIN { sub bar : Lion { print "Oh my!\n"; } print STDERR "\nDumping " . \&bar . " in BEGIN block after compilati +on.\n"; #Devel::Peek::Dump(\&bar); my $sName = getOriginalCodeRefName(__PACKAGE__, \&bar); $sName = 'undef' unless defined($sName); print STDERR "Name via Thilosophy method: <$sName>\n"; }

outputs...

Dumping CODE(0x81a8e60) in MODIFY_CODE_ATTRIBUTES: Warning: could not find name for CODE(0x81a8e60) at Monks/Snippet.pm l +ine 23. Name via Thilosophy method: <undef> Dumping CODE(0x81a8e60) in BEGIN block after compilation. Name via Thilosophy method: <bar>

I am more than a little surprised that it is so difficult to use MODIFY_CODE_ATTRIBUTES to redefine functions. When I scanned the web last night in response to your query on attribute handling it seemed that one of the major use case for attributes is decorating functions. It seems a little strange to add a feature that can't be used for one of its major use cases.

Best, beth

Update: fixed some typos, added comment about blocks, some clarifications.

Further update: Just reread the Perl documentation for attributes and I think I finally understand what the last sentence of this quote might mean. Do you suppose "cloned copies ... used as closures" means decorated functions?... and the reason for this non-implementation being that the coderef is only partially set up? (i.e. not part yet of the symbol table)

WARNING: the mechanisms described here are still experimental. Do not rely on the current implementation. In particular, there is no provision for applying package attributes to 'cloned' copies of subroutines used as closures.

It does appear that the only way to get decorated functions is to set up CHECK handlers or do ugly things like store a hash keyed by code ref and then match up the names and the coderefs later on, a la Class::Declare.


In reply to Re^2: Is there a way to access the name under which a subroutine was first defined? by ELISHEVA
in thread Is there a way to access the name under which a subroutine was first defined? by ELISHEVA

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (2)
    As of 2020-10-24 18:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (246 votes). Check out past polls.

      Notices?