http://qs321.pair.com?node_id=214030

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;