in reply to [SOLVED]: eval in perl package

Well, func4 is a closure. Perl sees that it uses a lexical variable from its declaration scope, so it keeps a link to that scalar value for the function to still be able to access it (and to prevent the scalar from being deleted at the end of the file). func3 holds no such link. Now I don't know what variable func3 acccesses, because the eval doesn't fail, but calling eval "$_[0]++" always returns 0 so it's not always the same var.

Instead of exporting a package variable I intended to return the value via 'eval' in a module function.
Honestly that sounds like a bad idea. Maybe you can use a hash instead.

Replies are listed 'Best First'.
Re^2: eval in perl package (Pad)
by LanX (Saint) on Mar 01, 2018 at 15:14 UTC
    > Perl sees that it uses a lexical variable from its declaration scope, so it keeps a link to that scalar value for the function to still be able to access it

    To elaborate further, each sub (and each block?) has hash like structures called "Pads", where closed over variables are referenced.

    This Pad is filled at compile time, and eval can only access lexical variables used in this scope.

    PadWalker allows to inspect this pad, or the pad of surrounding scopes.

    (PadWalker could also be used as a workaround for the OP)

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