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

Hi

it's possible to use PadWalker::var_name( LEVEL, VAR_REF ) to get the original name of a reference in the callers scope.

Question: can I use the B back-end to achieve the same by calling methods with pure Perl (aka without dependency to a° XS module)?

EDIT

ehm ... turns out the monastery is my virtual rubber duck

B::Xref is in pure Perl and is capable to inspect the lexicals of a sub. So the answer should be yes, provided I'm able to compare the refs too.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) non-core

  • Comment on Getting name of a lexical variable without PadWalker?

Replies are listed 'Best First'.
Re: Getting name of a lexical variable without PadWalker?
by dave_the_m (Monsignor) on Jul 15, 2020 at 21:10 UTC
    Erm, B::Xref is dependent on B, which is an XS module (albeit bundled with the perl distribution).

    Dave.

      > bundled with the perl distribution

      yep, correct, this I meant.

      hence

      > > without dependency to a non-core XS module

      updated

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      > Erm, B::Xref is dependent on B,

      B::Xref itself is written in Perl, necessary methods are imported from B

      > which is an XS module (albeit bundled with the perl distribution).

      B is advertised as "Perl's Compiler Backend" and bundled in core.

      Hence no dependency risk, using it is far safer as recompiling an XS module.

      PadWalker has bugs, a Pure Perl solution could be updated more often.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery