in reply to [SOLVED]: eval in perl package

Prelude

I stand corrected, Eily and hippo are right, the sub can only see closed over variables, I didn't try the code, mea culpa

update end

I don't understand your question, what is natively supposed to mean?

Both func return the same $x.

$y = $myscalar; is the only important but non effective difference.

update getter

the straightforward way to implement this is an exported getter

sub get_myscalar { return $myscalar }

If you really have too many individual lexicals to be handled by getters, you probably want to hold them in a hash instead.

update %exported_lex

my %exported_lex ={ myscalar => \$myscalar, myscalar2 => \$myscalar2, } sub get_lex_ref { return $exported_lex{$_[0]} } sub get_lex_val { return ${$exported_lex{$_[0]}} }

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

Replies are listed 'Best First'.
Re^2: eval in perl package (updated getter %exported_lex) )
by MarcusE (Initiate) on Mar 01, 2018 at 16:12 UTC

    Hi Rolf

    with 'natively' I meant that I use variable &myscalar explicitely and not just indirectly in the eval statement. When reading all comments I suppose the correct wording would be "in lexical context"

    Thanks to all for explanation and background information.

    Cheers Marcus

      Hello Marcus,

      Welcome to the monastery!

      And your code is an excellent example to demonstrate how closures internally work.

      Thanks! :)

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