Help with "require" and importing constants

Discussion in 'Perl Misc' started by Henry Law, Sep 24, 2006.

  1. Henry Law

    Henry Law Guest

    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
    Henry Law, Sep 24, 2006
    #1
    1. Advertising

  2. 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.
    Brian McCauley, Sep 24, 2006
    #2
    1. Advertising

  3. Henry Law

    Bob Walton Guest

    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
    Bob Walton, Sep 24, 2006
    #3
  4. Henry Law

    Ben Morrow Guest

    Quoth :
    > 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.
    Frank Herbert, 'Dune'
    Ben Morrow, Sep 25, 2006
    #4
  5. Henry Law

    Henry Law Guest

    Brian McCauley wrote:
    > Henry Law wrote:
    >> 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.


    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
    Henry Law, Sep 25, 2006
    #5
  6. Mumia W. (reading news) wrote:
    > 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 }


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

    sub DATA_MAX () { 100000 }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 25, 2006
    #6
  7. 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.
    Brian McCauley, Sep 27, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. plb
    Replies:
    2
    Views:
    340
  2. Nathan Harmston

    Importing module of data dicts and constants

    Nathan Harmston, Nov 25, 2006, in forum: Python
    Replies:
    1
    Views:
    228
    Fuzzyman
    Nov 25, 2006
  3. Matt_D
    Replies:
    5
    Views:
    266
    Matt_D
    Dec 10, 2007
  4. Chad M.
    Replies:
    2
    Views:
    124
    Chad M.
    Mar 27, 2009
  5. Replies:
    56
    Views:
    684
    alex23
    Dec 27, 2012
Loading...

Share This Page