Hi

motivation

An old trick is that when seeking the DATA filehandle to the beginning it's possible to read the sourcecode even before __DATA__ starts.

But with source in multiple files, modules and packages this becomes hairy.

approach

I was looking for a possibility to grab the source right away when its parsed.

Thats why I extended this example code from Attribute::Handlers to access DATA when CHECKing and print the line of the sub foo1() declaration:

package LoudDecl; use Attribute::Handlers; no warnings; sub Loud :ATTR { my ($package, $symbol, $referent, $attr, $data, $phase, $filename , $linenum) = @_; print STDERR ref($referent), " ", *{$symbol}{NAME}, " ", "($referent) ", "was just declared ", "and ascribed the ${attr} attribute ", "with data ($data)\n", "in phase $phase\n", "in file $filename at line $linenum\n"; my $tell=tell DATA; seek DATA,0,0; while (<DATA>) {print "> $_" if $.==$linenum }; seek DATA,$tell,0; } sub foo1 :Loud {print "HAHA"}; __DATA__ huhu

problem

well it works but I get a weird error from Attribut::Handlers, which only goes away after commenting out the while(<DATA>) line. I'm confused...

OUTPUT

CODE foo1 (CODE(0x8eba020)) was just declared and ascribed the Loud a +ttribute with data () in phase CHECK in file /home/lanx/perl/exp/tell.pl at line 23 > sub foo1 :Loud {print "HAHA"}; Can't use an undefined value as an ARRAY reference at /usr/share/perl/ +5.10/Attribute/Handlers.pm line 185, <DATA> line 31. INIT failed--call queue aborted, <DATA> line 31. Can't use an undefined value as an ARRAY reference at /usr/share/perl/ +5.10/Attribute/Handlers.pm line 185, <DATA> line 31. END failed--call queue aborted, <DATA> line 31.

question

What's going on, what can I do alternatively?

Cheers Rolf

UPDATE: I have the same error when directly opening the source file... it seems Attribute::Handlers is buggy...

In reply to Reading sourcecode at parsing/compilation (CHECK) time? by LanX

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.