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

if ($^O eq 'MSWin32') { # eval { # use Win32::Process; require Win32::Process; import Win32::Process; my $obj; if (Win32::Process::Open($obj, Win32::Process::GetCurrentProce +ssID(), 0)) { $obj->SetPriorityClass(THREAD_PRIORITY_BELOW_NORMAL); } }; }

Hello, everyone. Sorry to my poor english. I was run the code above and I got a error message:

Bareword "THREAD_PRIORITY_BELOW_NORMAL" not allowed while "strict subs" in use at main2.pl line 466.
Execution of main2.pl aborted due to compilation errors.

But "use Win32::Process" will be OK. what's wrong?

  • Comment on require Win32::Process, barewords "THREAD_PRIORITY_BELOW_NORMAL" not allowed?
  • Download Code

Replies are listed 'Best First'.
Re: require Win32::Process, barewords "THREAD_PRIORITY_BELOW_NORMAL" not allowed?
by cdarke (Prior) on Sep 21, 2009 at 15:42 UTC
    The problem is that you are using require, which is executed at run-time. That means "THREAD_PRIORITY_BELOW_NORMAL" will not be known at compile-time, hence the error.

    I think I see why you are using require, because you want a conditional module use. I suggest you use (pun) the if pragma. For example, here is some code from one of my modules which runs under Linux and Windoze:
    use if ($^O eq "MSWin32"), 'Win32::Process'; use if ($^O eq "MSWin32"), 'Win32::SearchPath'; use if ($^O ne "MSWin32"), 'POSIX'; use if ($^O ne "MSWin32"), 'POSIX' => ':sys_wait_h'; # Hack to allow compilation under Unix # (NORMAL_PRIORITY_CLASS and INFINITE are Win32 only) use if ($^O ne "MSWin32"), 'constant' => 'NORMAL_PRIORITY_CLASS'; use if ($^O ne "MSWin32"), 'constant' => 'INFINITE';
    The constants (similar to that you need) have to be defined for non-Windows platforms. They need no value, they are there to keep the compiler happy.
Re: require Win32::Process, barewords "THREAD_PRIORITY_BELOW_NORMAL" not allowed?
by ikegami (Patriarch) on Sep 21, 2009 at 15:21 UTC
    You can't omit parens on subs that haven't been declared. Either add parens to THREAD_PRIORITY_BELOW_NORMAL, or import the constant before THREAD_PRIORITY_BELOW_NORMAL is compiled (in which case you'll get true constant).
Re: require Win32::Process, barewords "THREAD_PRIORITY_BELOW_NORMAL" not allowed?
by Anonymous Monk on Sep 21, 2009 at 15:23 UTC
    But "use Win32::Process" will be OK. what's wrong?

    You forgot to use diagnostics;

    Bareword "THREAD_PRIORITY_BELOW_NORMAL" not allowed while "strict subs" in use at main2.pl line 466. Execution of main2.pl aborted due to compilation errors. (#1)
    (F) With "strict subs" in use, a bareword is only allowed as a subroutine identifier, in curly brackets or to the left of the "=>" symbol. Perhaps you need to predeclare a subroutine?
    Use strict warnings and diagnostics or die
    ## predeclare sub THREAD_PRIORITY_BELOW_NORMAL;
      The declaration's prototype has to match the definition's prototype.
      sub THREAD_PRIORITY_BELOW_NORMAL();

      Alternative:

      BEGIN { if ($^O eq 'MSWin32') { require Win32::Process; import Win32::Process; } } if ($^O eq 'MSWin32') { my $pid = Win32::Process::GetCurrentProcessID(); if (Win32::Process::Open(my $obj, $pid, 0)) { $obj->SetPriorityClass(THREAD_PRIORITY_BELOW_NORMAL); } }
Re: require Win32::Process, barewords "THREAD_PRIORITY_BELOW_NORMAL" not allowed?
by appleii (Novice) on Sep 21, 2009 at 16:24 UTC
    Thanks, It works now :)