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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Perl attributes are typically handled at CHECK time - between compile time (BEGIN) and INIT time, and way before run time. Your eval happens at run time, so too late to effect attribute processing.

Hm. That doesn't ring true.

Just because it is the main scripts runtime, doesn't prevent the eval use'd script from having its own BEGIN & END times. After all, it still has to be compiled.

Given junk.pm:

package junk; sub BEGIN { printf "Hi from %s BEGIN\n", __PACKAGE__ } sub CHECK { printf "Hi from %s CHECK\n", __PACKAGE__ } sub INIT { printf "Hi from %s INIT\n", __PACKAGE__ } sub END { printf "Hi from %s END\n", __PACKAGE__ } 1;

This script that eval's it into being:

#! perl -slw use strict; print "Main runtime: About to eval use junk"; <STDIN>; eval "use junk"; print "Main runtime: Just eval'd use junk"; <STDIN>;

I get:

C:\test>junk Main runtime: About to eval use junk Hi from junk BEGIN Too late to run CHECK block at junk.pm line 4, <STDIN> line 1. Too late to run INIT block at junk.pm line 5, <STDIN> line 1. Main runtime: Just eval'd use junk Hi from junk END

So, if the attribute handling can be done at BEGIN time, then it is quite simple to wrap the attributed function over in a wrapper that did the tracing,

Tweak junk.pm above to be::

package junk; use Attribute::Handlers; no warnings 'redefine'; sub TRACE : ATTR(CODE,BEGIN) { my ($pkg, $sym, $ref, $attr, $data, $phase, $file, $line) = @_; *{ $sym } = sub { warn "$file($line) called with [@_]\n"; my( @rc )= &$ref; warn "$file($line): returning [ @rc ]\n"; } } sub doStuff :TRACE { print "junk::doStuff says hi"; } 1;

And main to be:

#! perl -slw use strict; print "Main runtime: About to eval use junk"; <STDIN>; eval "use junk"; print "Main runtime: Just eval'd use junk; calling doStuff()"; <STDIN> +; junk::doStuff( 1..10 );

And you get:

C:\test>junk Main runtime: About to eval use junk Main runtime: Just eval'd use junk; calling doStuff() junk.pm(16) called with [1 2 3 4 5 6 7 8 9 10] junk::doStuff says hi junk.pm(16): returning [ 1 ]

which is kind of cool. (Shame the function name isn't provided to the attribute handler.)

It'd be better if installing the TRACE attr into UNIVERSAL would then apply the wrappers to runtime loaded modules; and better yet if you you could use a "trace handler" package once in main and have it apply itself everywhere.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

@_

In reply to Re^2: Core module: Attribute::Handlers behaviour by BrowserUk
in thread Core module: Attribute::Handlers behaviour by enigma

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 making s'mores by the fire in the courtyard of the Monastery: (8)
    As of 2014-12-18 04:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (41 votes), past polls