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 | |
by billgdev (Initiate) on Feb 18, 2015 at 21:25 UTC | |
by BrowserUk (Patriarch) on Feb 18, 2015 at 22:33 UTC | |
by tye (Sage) on Feb 19, 2015 at 02:16 UTC | |
by tye (Sage) on Feb 19, 2015 at 04:13 UTC |