in reply to Threads and Shared Variables

Don't embed function definitions (eg. sub) inside other blocks.

See For loop and Sub Routines for the same problem without threads.


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.

Replies are listed 'Best First'.
Re^2: Threads and Shared Variables
by hermes1908 (Novice) on Jul 04, 2013 at 20:40 UTC
    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.
      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