Home > Archive > PERL Miscellaneous > September 2006 > Help with "require" and importing constants
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help with "require" and importing constants
|
|
| Henry Law 2006-09-24, 7:00 pm |
| 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.
--
Henry Law <>< Manchester, England
| |
| Brian McCauley 2006-09-24, 7:00 pm |
|
Henry Law wrote:
> 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.
| |
| Bob Walton 2006-09-24, 7:00 pm |
| 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.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
| |
| Henry Law 2006-09-25, 3:59 am |
| Brian McCauley wrote:
> Henry Law wrote:
>
> 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 ...
> but you are not loading the module
> that declares that function until _after_ the compilation phase.
> If you do call Win32::File->import then you can write ARCHIVE() rather
> than needing to say Win32::File::ARCHIVE().
--
Henry Law <>< Manchester, England
| |
| Mumia W. (reading news) 2006-09-25, 3:59 am |
| On 09/25/2006 02:03 AM, Henry Law wrote:
>
> [...] 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 }
--
paduille.4058.mumia.w@earthlink.net
| |
| John W. Krahn 2006-09-25, 3:59 am |
| Mumia W. (reading news) wrote:
> On 09/25/2006 02:03 AM, Henry Law wrote:
>
> 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
--
use Perl;
program
fulfillment
| |
| Ben Morrow 2006-09-25, 6:59 pm |
|
Quoth news@lawshouse.org:
> 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
--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
benmorrow@tiscali.co.uk Frank Herbert, 'Dune'
| |
| Brian McCauley 2006-09-27, 4:00 am |
|
Henry Law wrote:
>
> 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.
|
|
|
|
|