Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Getting a code ref to main

by diotalevi (Canon)
on Nov 19, 2002 at 04:47 UTC ( [id://214030]=perlquestion: print w/replies, xml ) Need Help??

diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way for a script to get a reference to the main sub? I was trying to do something like &main;, &:: and &; but obviously none of those worked or I wouldn't be asking now. I'd like to get a code reference to the currently executing contex for use in peeking at the innards of stuff like (?{1}). I also tried using judo like B's main_cv but that didn't get me anywhere either - it just produces an exception. Help? The code is an example of what I'm actually doing. I'm thinking that if I can get a proper code reference that this will work better since perhaps however main_cv works it isn't getting whatever I'm looking for (or then why else would it produce exception errors?)

use B 'main_cv'; use B::Deparse; $deparse = B::Deparse->new; # This prints "{}\n" which doesn't seem right print $deparse->deparse_sub(main_cv), $/; $qr = qr|(?:.(?{print $deparse->deparse_sub(main_cv), $/}))+|; for (0 .. 9) { print "$_\n"; $text = ' ' x $_; $text =~ $qr; }
__SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

Replies are listed 'Best First'.
Re: Getting a code ref to main
by Courage (Parson) on Nov 19, 2002 at 07:37 UTC
    main_cv just returns Perl internal variable PL_main_cv which in turn serves special purposes.

    As I see a situation, it is a very special *CV value that can not be deparsed (like can not be deparsed, say, sort internal function), and there should be special effort be made to make main_cv deparse-able, which, in turn, seems do-able to me, but not implemented nowadays.

    Best wishes,
    Courage, the Cowardly Dog

      Well... ok. So then if main_cv isn't good enough is there a way to get a proper code reference to the main() subroutine or whatever the heck it's called?

      __SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;
Re: Getting a code ref to main
by chromatic (Archbishop) on Nov 19, 2002 at 21:07 UTC

    To my knowledge, it's not really a cv at all, it's just the root op of the optree. The best I can suggest is main_root:

    use B 'main_root'; use B::Deparse; my $deparse = B::Deparse->new(); print $deparse->( main_root, 0 );

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-28 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found