in reply to Variable will not stay shared in subroutine

For the benefit of others googling this issue, am I correct to assume that what most people *mean* when they hit this problem in their (usually different to this example) code, is to be doing the following instead? our $self = shift; I gather this is a very common issue in mod_perl, where I'm hoping the above is the correct solution to what the coder probably mean to be a global variable.
  • Comment on Re: Variable will not stay shared in subroutine

Replies are listed 'Best First'.
Re^2: Variable will not stay shared in subroutine
by choroba (Cardinal) on Aug 29, 2014 at 17:17 UTC
    The correct solution is not to declare a named subroutine inside a named subroutine.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I found this thread when looking for explanation for this message. In my case the solution is using "our" instead of "my". As a side effect the variable is a global with limited visibility.
        You've just treated the symptoms, not the cause. In Perl, it makes no sense to define a named subroutine inside a named subroutine, as the nested subroutine is still globally visible.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        If you really need a nested subroutine for the current scope only, then better use an anonymous one, like already described here by tilly.

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