'use' command being executed

J

jhavero

The 'use' command below tries to run when this is executed from Unix
so I get an error that it cannot find the Win32::File module. If I
comment out this 'use' line the script works in Unix and the 'print
"Unix"' statement runs and the 'print "Windows"' statement never runs.

How can I stop it from running the 'use' command when run from Unix.

# Main
..
..
..
my %FINDOPTIONS = (
'wanted' => \&entry,
'no_chdir' => 1
);
..
..
..
find(\%FINDOPTIONS,@COMPONENTS);

sub entry {

if($^O eq 'MSWin32') {
#Windows
use Win32::File qw/ GetAttributes SetAttributes /;
print "Windows\n";
}
} else {
#unix
print "Unix\n";
}

} #end sub entry
 
J

Jim Gibson

jhavero said:
The 'use' command below tries to run when this is executed from Unix
so I get an error that it cannot find the Win32::File module. If I
comment out this 'use' line the script works in Unix and the 'print
"Unix"' statement runs and the 'print "Windows"' statement never runs.

How can I stop it from running the 'use' command when run from Unix.

sub entry {

if($^O eq 'MSWin32') {
#Windows
use Win32::File qw/ GetAttributes SetAttributes /;
print "Windows\n";
}
} else {
#unix
print "Unix\n";
}

} #end sub entry

Replace the use line with the following (untested):

require Win32::File;
Win32::File->import( qw( GetAttributes SetAttributes ));

See 'perldoc -f use' and note that the above two lines are the
equivalent to the use line but without the BEGIN { } block. A BEGIN
forces unconditional execution of thestatements (and inclusion of the
module) at compile time.
 
A

Andrzej Adam Filip

jhavero said:
The 'use' command below tries to run when this is executed from Unix
so I get an error that it cannot find the Win32::File module. If I
comment out this 'use' line the script works in Unix and the 'print
"Unix"' statement runs and the 'print "Windows"' statement never runs.

How can I stop it from running the 'use' command when run from Unix.

# Main
.
.
.
my %FINDOPTIONS = (
'wanted' => \&entry,
'no_chdir' => 1
);
.
.
.
find(\%FINDOPTIONS,@COMPONENTS);

sub entry {

if($^O eq 'MSWin32') {
#Windows
use Win32::File qw/ GetAttributes SetAttributes /;
print "Windows\n";
}
} else {
#unix
print "Unix\n";
}

} #end sub entry

1) use "require" instead of "use"
2) use full names of functions with package prefix
Win32::File::GetAttributes
 
J

Jürgen Exner

jhavero said:
The 'use' command below tries to run when this is executed from Unix
so I get an error that it cannot find the Win32::File module. If I
comment out this 'use' line the script works in Unix and the 'print
"Unix"' statement runs and the 'print "Windows"' statement never runs.

How can I stop it from running the 'use' command when run from Unix.

You cannot because use() is executed at compile time, i.e. before any
if() is evaluated.

However you can step back to the components of use() (see 'perldoc -f
use') and use require() instead of use(). require() is executed at
runtime, thus you can prevent its execution in an if() statement.

jue
 
J

Justin C

The 'use' command below tries to run when this is executed from Unix
so I get an error that it cannot find the Win32::File module. If I
comment out this 'use' line the script works in Unix and the 'print
"Unix"' statement runs and the 'print "Windows"' statement never runs.

unless ($os eq "MSWin32") {
use ...
}

Now you just need to figure out what your operating system is. I'll give
you a clue, it's a frequently asked question.

Justin.
 
U

Uri Guttman

JC> unless ($os eq "MSWin32") {
JC> use ...
JC> }

JC> Now you just need to figure out what your operating system is. I'll give
JC> you a clue, it's a frequently asked question.

and did you test this? did you read the other posts in the thread about
why that is wrong? that use line will ALWAYS execute regardless of the
OS involved.

uri
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top