in reply to Re: Threads and Shared Variables
in thread Threads and Shared Variables

Thanks, but I'm still unclear as to why this is happening. Since the $progref variable shares the same scrope with the sub, I don't understand why the sub can't see it. I've read the relevant section of perlsub, and as far as I can tell this should work.

Replies are listed 'Best First'.
Re^3: Threads and Shared Variables
by BrowserUk (Patriarch) on Jul 04, 2013 at 21:11 UTC
    and as far as I can tell this should work.

    It doesn't. And it won't whether you understand it or not.

    In Perl: DO NOT DEFINE SUBROUTINES WITHIN NESTED SCOPES!

    Weird bugs happen whether threads are involved or not. And when threads are involved, they can be even weirder. So don't do it.

    I could probably eventually explain why it doesn't work, but then I would have to think about it and probably do some experiments. But as there is no good reason to declare subs in async blocks; and many good reasons not to, I cannot be bothered to work out what does or does not work.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      If it's just a quirk then I can accept that, I was just trying to establish whether or not I missed something in the documentation (I can't find anything about not nesting subroutines). My reasoning behind doing this was not to pollute the package namespace. I am used to being able to do this in other languages (javascript).

      Thanks for the advice,
      Hermes
        (I can't find anything about not nesting subroutines)

        See http://perldoc.perl.org/perlref.html#Function-Templates and read the three paragraphs where the first starts "Access to lexicals that change over time...".


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        Apart from the experimental lexical subs introduced with 5.18, any named function created with sub (even those that are nested in an inner scope) become part of a package's namespace, so you can't avoid namesapce pollution in this way.