Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^4: Introspecting the current Perl file via DATA, even w/o DATA? (perl5db.pl)

by LanX (Saint)
on Feb 05, 2016 at 15:14 UTC ( [id://1154484]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Introsepcting the current Perl file via DATA, even w/o DATA?
in thread Introspecting the current Perl file via DATA, even w/o DATA?

> Anyway, let us know if you'll find something in perl5db.pl or somewhere else.

see http://cpansearch.perl.org/src/SHAY/perl-5.22.1/lib/perl5db.pl

line 405

=head4 C<@dbline> Local alias to the magical line array, C<@{$main::{'_<'.$filename}}> , supplied by the Perl interpreter to the debugger. Contains the source.

and line 176

=head1 DATA STRUCTURES MAINTAINED BY CORE There are a number of special data structures provided to the debugger + by the Perl interpreter. The array C<@{$main::{'_<'.$filename}}> (aliased locally to C<@dbline> via glob assignment) contains the text from C<$filename>, with each element corresponding to a single line of C<$filename>.

Seems to be magic. Dunno if it depends on debug flags or just the context of the DB package like other magic (e.g. @DB::args )

Anyway, let us know if you'll find something details about that magic. ;-).

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^5: Introsepcting the current Perl file via DATA, even w/o DATA? (perl5db.pl)
by Anonymous Monk on Feb 05, 2016 at 16:11 UTC
    Anyway, let us know if you'll find something details about that magic. ;-)
    You're so lazy, LanX ;) I'm sure you could do it yourself, but if you insist...
    # main.pl BEGIN { $^P = 0x400; } use test ();
    and
    # test.pm my @source = @{ $main::{ '_<' . __FILE__ } }; print for @source; 1;
    Now, the thing is, you have to set $^P before the file (test.pm in this case) is even opened. So you can't do it inside test.pm itself, I don't think...

    Is that good enough for you?

      I found this interesting so I played with it briefly...

      # main.pl my @source = @{ $main::{ '_<' . __FILE__ } }; shift @source; # get rid of sub DB::DB{}; print for @source;
      and
      PERL5DB="sub DB::DB{}" perl -d main.pl

      Output:

      my @source = @{ $main::{ '_<' . __FILE__ } }; shift @source; # get rid of sub DB::DB{}; print for @source;

      Update: I had found @{$main::{'_<'.$filename}} yesterday but didn't have time to play with it.

      Update 2: Added shift.

        > Added shift.

        as a side note: line numbers start with 1, so changing the content of the 0th element may be the better strategy. ( Otherwise looking up a line will include a cumbersome -1 operation.)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

      > Is that good enough for you?

      yes, I knew I can count on your collaboration! ;)

      > Now, the thing is, you have to set $^P before the file (test.pm in this case) is even opened.

      I think one could trick that², the question is rather about other penalties included.

      ... let's have a look into perlvar#$^P ...

      x400 Save source code lines into @{"_<$filename"} .

      OK this doesn't look to expensive ... benchmark needed.

      (too busy now ;-)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      footnotes

      ²) for instance IIRC it's possible to configure global setting for your perl interpreter.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1154484]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-19 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found