in reply to Re^2: How to access a variable inside subroutine?
in thread How to access a variable inside subroutine?

Side note: be careful doing this with a my variable because some weird things can happen with nested subs referencing lexicals from the enclosing scope. Look for Variable "%s" will not stay shared in perldiag.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^4: How to access a variable inside subroutine?
by LanX (Saint) on Sep 06, 2020 at 05:55 UTC
    I wanted to give the same warning and I'm not sure this is less of a problem with state

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

      It isn't a problem with state.

      It's only a problem with my because a new SV is allocated for each call of the outer sub, but the inner sub only closed over the first one.

        In this case maybe, I'm sure one can also produce such problems with state.

        A named subroutine is created at compile time, putting them into other subs or loops means begging for trouble.

        I've never been a big fan of state, there are not many patterns (if any) which can't be implemented with closed over my declarations.

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