简体   繁体   中英

How do I conditionally use threads in a perl script?

I have a perl script that I've developed that runs on hosts that have different versions of perl, sometimes compiled with threads and sometimes without.

I have a

use if $Config{"useithreads"}, "threads";

And all of my thread-specific code is in similar conditionals.

However, during the compile phase, perl still chokes on threads::all, threads::running, etc.

How can I unsure my script runs on both threaded and non-threaded perls?

 [ worr on worr-mn1 ] ( manage_usr_local_admin ) % perl -c acct_mgr_ng.pl
Bareword "threads::joinable" not allowed while "strict subs" in use at acct_mgr_ng.pl line 117.
BEGIN not safe after errors--compilation aborted at acct_mgr_ng.pl line 541.

When threads is loaded, perl knows that threads::all (and friends) is a subroutine call, even without parentheses or & ; since threads may not be loaded, just explicitly call it with parentheses: threads::all()

use statements are parsed at compile-time. You want to use require or Module::Load to pull in modules conditionally at run-time.

Something like this should work:

use Module::Load ();
if($Config{'useithreads'}) {
    Module::Load::load("threads");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM