billgdev has asked for the wisdom of the Perl Monks concerning the following question:

use Module::Load::Conditional qw[can_load]; # Are we running threads? my $grThreaded = eval 'use threads; 1'; # If so, load thread modules my ($grThreadModules) = { 'threads::shared' => undef, 'Thread::Semaphore' => undef, }; if ( $grThreaded ) { unless ( can_load( modules => $grThreadModules ) ) { print "ERROR: Cannot load threaded modules in $0. Exiting."; exit(); } } use strict; my($gnWarning); if ( $grThreaded ) { # Does not work at all: Undefined subroutine &main::share # share($gnWarning); # This works in the debugger # However, if used in production: # Argument to share needs to be passed as ref # threads::shared::share($gnWarning); # This works at run time # However, if used under the debugger: # Type of arg 1 to threads::shared::share must be one of [$@%] # (not single ref constructor) threads::shared::share(\$gnWarning); }

In the code, I found that just trying to declare a variable as shared fails as being an undefined subroutine.

If I give the full variable module/name, it will work, but if I try to use the Perl debugger, I have to use a reference to the variable.

So I'm stumped as to the behavior. I'm running perl 5.20.1 under Linux.

Thanks, Bill

Replies are listed 'Best First'.
Re: Problem conditionally making variables shared if using threads
by BrowserUk (Patriarch) on Feb 18, 2015 at 20:06 UTC
    If I give the full variable module/name, it will work,

    You haven't used threads::shared in the current scope (package), so how could it have exported its functions into that scope?

    I try to use the Perl debugger, I have to use a reference to the variable.

    You always have to pass a reference to threads::shared::share(); unless you disable the prototype.


    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". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      You haven't used threads::shared in the current scope (package)

      Isn't that accomplished by using use Module::Load::Conditional qw[can_load];?

      You always have to pass a reference to threads::shared::share(); unless you disable the prototype

      Disable the prototype? I'm not sure what you mean by that. Explicitly using &share($gnWarning) makes no difference, if that's what you mean.

      If you know of any way to configure a program to use threads based on the perl executable being compiled with it, and not use it otherwise, other than the method I'm trying, I'd be interested to know.

        From the Module::Load::Conditional POD for can_load:

        autoload

        This controls whether imports the functions of a loaded modules to the caller package. The default is no importing any functions.

        You haven't asked for the exported functions to be imported into your package.

        Disable the prototype? I'm not sure what you mean by that. Explicitly using &share...

        You got it :)

        Explicitly using &share($gnWarning) makes no difference,

        It won't if share() has not been imported into your package; unless you fully qualify it.

        If you know of any way to configure a program to use threads based on the perl executable being compiled with it, and not use it otherwise, other than the method I'm trying, I'd be interested to know.
        use Config; if( $Config{ useithreads } eq 'define' ) { ## compiled with ithreads require threads; threads->import; require threads::shared; threads::shared->import } else { ## not }

        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". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked