Help with "require" and importing constants

H

Henry Law

There's an easy answer to this, I'm sure, but an hour of Googling and
experimenting has not led me to it. Can someone help?

I need to include the module Win32::File if my code is running on
Windows, but not on Linux. Job for the "require" statement, I thought.
The module exports a set of constants (ARCHIVE being my example), but
not, apparently, when "require" is used. How do I code this simple thing?

Code that does what I want on a Win32 system:
#! /usr/bin/perl
use strict; use warnings;
use Win32::File;
#require Win32::File;
#Win32::File->import qw(ARCHIVE);
printf "Archive constant from Win32::File: %x\n", ARCHIVE;

Commenting out "use" and making the "require" statement active gives
Bareword "ARCHIVE" not allowed while "strict subs" in use at ...

Also included above, commented out, is an unscholarly attempt to use the
"import" method, based on a Perl Cookbook recipe, but (a) it gives the
same error, and (b) the constants are exported automatically by
Win32::File without being specified, so I shouldn't have to code
"import" anyway.
 
B

Brian McCauley

Henry said:
There's an easy answer to this, I'm sure, but an hour of Googling and
experimenting has not led me to it. Can someone help?

I need to include the module Win32::File if my code is running on
Windows, but not on Linux. Job for the "require" statement, I thought.
The module exports a set of constants (ARCHIVE being my example), but
not, apparently, when "require" is used. How do I code this simple thing?

Simply change ARCHIVE to Win32::File::ARCHIVE().

i.e. full name, because require() doesn't call import() and make it
explicit to the compiler that you are talking about a function because
it can't know if you didn't load the module during the compilation
phase.
Code that does what I want on a Win32 system:
#! /usr/bin/perl
use strict; use warnings;
use Win32::File;
#require Win32::File;
#Win32::File->import qw(ARCHIVE);
printf "Archive constant from Win32::File: %x\n", ARCHIVE;

Commenting out "use" and making the "require" statement active gives
Bareword "ARCHIVE" not allowed while "strict subs" in use at ...

Also included above, commented out, is an unscholarly attempt to use the
"import" method, based on a Perl Cookbook recipe, but (a) it gives the
same error,

You get the error because you are trying to rely on information to
travelling backwards in time. (A technique also known as the
"temporally global variable"). You want the compiler to know (during
the compilation phase) that the bare word ARCHIVE (without a ()) is a
to be compiled as a function call but you are not loading the module
that declares that function until _after_ the compilation phase.
and (b) the constants are exported automatically by
Win32::File without being specified,

Er no, It is Win32::File->import that doing the exporting. Your phrasse
"without being specified" in this context mean "even if the argument
list passed to Win32::File->import() is empty".

If you do call Win32::File->import then you can write ARCHIVE() rather
than needing to say Win32::File::ARCHIVE().
so I shouldn't have to code "import" anyway.

perldoc -f use

Note that use() is eqivalent to require and import in a BEGIN {} block.
 
B

Bob Walton

Henry Law wrote:
....
I need to include the module Win32::File if my code is running on
Windows, but not on Linux. Job for the "require" statement, I thought.
The module exports a set of constants (ARCHIVE being my example), but
not, apparently, when "require" is used. How do I code this simple thing?
....
To tell what your platform is, check out:

perldoc perlvar

particularly the section about $^O

Something like:

BEGIN{if($^O eq 'MSWin32'){require Win32::File;import Win32::File;}}
use Data::Dumper;
print Dumper(ARCHIVE);

might work. On my Win32 system, this prints '32'. If I change MSWin32
to something else, it prints 'ARCHIVE', indicating that the module did
not load.
 
B

Ben Morrow

Quoth (e-mail address removed):
There's an easy answer to this, I'm sure, but an hour of Googling and
experimenting has not led me to it. Can someone help?

I need to include the module Win32::File if my code is running on
Windows, but not on Linux. Job for the "require" statement, I thought.
The module exports a set of constants (ARCHIVE being my example), but
not, apparently, when "require" is used. How do I code this simple thing?

See if.pm on CPAN.

Ben
 
H

Henry Law

Brian said:
Simply change ARCHIVE to Win32::File::ARCHIVE().

i.e. full name, because require() doesn't call import() and make it
explicit to the compiler that you are talking about a function because
it can't know if you didn't load the module during the compilation
phase.

Thanks, Brian. I tried almost every combination except this one and the
correct use of "import" as you've described below. Works fine.

But follow-up questions if I may. ARCHIVE isn't a function, it's a
constant. So (a) why can I refer to it as a function like this, (b) why
does that give the required behaviour, and (c) do I infer correctly that
constants and functions have the same namespace, and that Win32::File
could not therefore have a function called ARCHIVE() as well as a constant?
You get the error because you are trying to rely on information to
travelling backwards in time. (A technique also known as the
"temporally global variable"). You want the compiler to know (during
the compilation phase) that the bare word ARCHIVE (without a ()) is a
to be compiled as a function call

As I say, this equivalence of functions calls and constants is very
confusing ...
 
J

John W. Krahn

Mumia said:
[...] ARCHIVE isn't a function, it's a constant. So (a) why can I
refer to it as a function like this, (b) why does that give the
required behaviour, and (c) do I infer correctly that constants and
functions have the same namespace, and that Win32::File could not
therefore have a function called ARCHIVE() as well as a constant?
[...]

In Perl, generally constants are implemented as functions, e.g.
sub DATA_MAX { 100000 }

See the "Constant Functions" section of perlsub. The correct way is:

sub DATA_MAX () { 100000 }



John
 
B

Brian McCauley

Henry said:
But follow-up questions if I may. ARCHIVE isn't a function, it's a
constant.

As others have pointed out, Perl constants _are_ functions. They have a
special attribute so that the compiler knows to call them at compile
time and simply compile in the value.

But since you are compiling the code that uses ARCHIVE before you load
the module that defines it, ARCHIVE cannot be compiled as a constant
in your code, it's just a simple function call.
 

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,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top