'use' command being executed

Discussion in 'Perl Misc' started by jhavero, Oct 22, 2009.

  1. jhavero

    jhavero Guest

    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
     
    jhavero, Oct 22, 2009
    #1
    1. Advertising

  2. jhavero

    Jim Gibson Guest

    In article
    <>,
    jhavero <> wrote:

    > 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.

    --
    Jim Gibson
     
    Jim Gibson, Oct 22, 2009
    #2
    1. Advertising

  3. jhavero <> wrote:
    > 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

    --
    [pl>en Andrew] Andrzej Adam Filip : :
    To believe your own thought, to believe that what is true for
    you in your private heart is true for all men -- that is genius.
    -- Ralph Waldo Emerson
     
    Andrzej Adam Filip, Oct 22, 2009
    #3
  4. jhavero <> wrote:
    >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ürgen Exner, Oct 23, 2009
    #4
  5. jhavero

    Justin C Guest

    On 2009-10-22, jhavero <> wrote:
    > 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.

    --
    Justin C, by the sea.
     
    Justin C, Oct 26, 2009
    #5
  6. jhavero

    Uri Guttman Guest

    >>>>> "JC" == Justin C <> writes:

    JC> On 2009-10-22, jhavero <> wrote:
    >> 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.


    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Oct 26, 2009
    #6
    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. bid
    Replies:
    1
    Views:
    423
    Srinivasa Reddy K Ganji
    Jul 21, 2003
  2. crjunk
    Replies:
    2
    Views:
    16,868
    crjunk
    Aug 8, 2003
  3. PERCIVAL BRAGG
    Replies:
    0
    Views:
    559
    PERCIVAL BRAGG
    Oct 15, 2003
  4. Michael
    Replies:
    0
    Views:
    558
    Michael
    Feb 18, 2004
  5. Chris Gorton

    atexit not being executed

    Chris Gorton, May 3, 2005, in forum: Python
    Replies:
    0
    Views:
    400
    Chris Gorton
    May 3, 2005
Loading...

Share This Page