Strange behavior of "use if" (a conditional "use" with the if module)

D

David Filmer

I have a program with this line of code:

use if( $Config{'osname'} =~ /Win/ ), 'Win32::process::Info';

Perl complains:

Too few arguments to `use if' (some code returning an empty list in list context?) at ...

However, if I change the regex operator to !~ then Perl is quite happy (the only change is replacing the equals with a bang).

Does anyone know why Perl is unhappy with =~ in my "use if" statement

thanks!
 
B

Bjoern Hoehrmann

* David Filmer wrote in comp.lang.perl.misc:
I have a program with this line of code:

use if( $Config{'osname'} =~ /Win/ ), 'Win32::process::Info';

Perl complains:

Too few arguments to `use if' (some code returning an empty list in list context?) at ...

If your osname actually matches /Win/ then you probably forgot to load
Config.pm. Otherwise, the expression returns an empty list, so there are
no arguments passed, just like the error message says. Use something
like `scalar($Config{'osname'} =~ /Win/)` to force a scalar context.
 
C

C.DeRykus

I have a program with this line of code:



use if( $Config{'osname'} =~ /Win/ ), 'Win32::process::Info';



Perl complains:



Too few arguments to `use if' (some code returning an empty list in list context?) at ...



However, if I change the regex operator to !~ then Perl is quite happy (the only change is replacing the equals with a bang).



Does anyone know why Perl is unhappy with =~ in my "use if" statement

With strict,warnings,(and more hints from diagnostics), you can hone in on what what 's going wrong:

If you forgot Config, there's a fatal warning from strict:

perl -Mstrict -wle ' use if( $Config{osname} =~ /Win/ ), "Win32::process::Info"'
Global symbol "%Config" requires explicit package name at
line 1.

Even if you forget Config and strict both, you get hints from warnings:

perl -Mstrict -wle ' use if( $Config{osname} =~ /Win/ ), "Win32::process::Info"'
Use of uninitialized value $Config{"osname"} in pattern
match (m//) at line 1.
Too few arguments to 'use if' (some code returning an empty list in list context?) ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top