unexpected EXTENDED_OS_ERROR on Windows

Discussion in 'Perl Misc' started by Griff, Aug 31, 2004.

  1. Griff

    Griff Guest

    hi there

    I'm running ActiveState Perl v5.6.1 on Windows XP.

    The code

    $cmdstr = "copy old.txt new.txt";
    system($cmdstr);
    print "$^E";

    produces the results
    ------------------------------------------
    1 file(s) copied.
    The system cannot find the file specified.
    ------------------------------------------
    When I check the files old.txt and new.txt, the file copy has actually worked.
    Why do I get this error message in EXTENDED_OS_ERROR ?
    (If I print the "$^E" variable before running the program, I can see it is empty.)

    Your assistance would be welcomed.

    Thanks, Griff
     
    Griff, Aug 31, 2004
    #1
    1. Advertising

  2. Griff

    Tore Aursand Guest

    On Tue, 31 Aug 2004 03:45:42 -0700, Griff wrote:
    > I'm running ActiveState Perl v5.6.1 on Windows XP.
    >
    > The code
    >
    > $cmdstr = "copy old.txt new.txt";
    > system($cmdstr);
    > print "$^E";
    >
    > [...]


    Why rely on non-portable methods? I don't have the answer to your
    question, but I have - most possibly - a solution to your problem.

    Use the File::Copy module which comes with Perl;

    #!/usr/bin/perl
    #
    use strict;
    use warnings;
    use File::Copy;

    copy( 'old.txt', 'new.txt' ) or die "Copy failed; $!\n";
    move( 'old.txt', 'new.txt' ) or die "Move failed; $!\n";

    Please read the documentation for the File::Copy module for more
    information and examples.


    --
    Tore Aursand <>
    "I know not with what weapons World War 3 will be fought, but World War
    4 will be fought with sticks and stones." (Albert Einstein)
     
    Tore Aursand, Aug 31, 2004
    #2
    1. Advertising

  3. Griff wrote:

    > system($cmdstr);
    > print "$^E";


    You should only inspect $! or $^E after system() if the return value
    from system() was -1.
     
    Brian McCauley, Aug 31, 2004
    #3
  4. Griff

    Paul Lalli Guest

    > $cmdstr = "copy old.txt new.txt";
    > system($cmdstr);
    > print "$^E";
    >
    > produces the results
    > ------------------------------------------
    > 1 file(s) copied.
    > The system cannot find the file specified.
    > ------------------------------------------
    > When I check the files old.txt and new.txt, the file copy has actually

    worked.
    > Why do I get this error message in EXTENDED_OS_ERROR ?
    > (If I print the "$^E" variable before running the program, I can see

    it is empty.)

    From perldoc perlvar:

    Under Win32, $^E always returns the last error
    information reported by the Win32 call
    "GetLastError()" which describes the last error from
    within the Win32 API. Most Win32-specific code will
    report errors via $^E. ANSI C and Unix-like calls
    set "errno" and so most portable Perl code will
    report errors via $!.

    Caveats mentioned in the description of $! generally
    apply to $^E, also. (Mnemonic: Extra error
    explanation.)

    That tells us that $^E is set to th elast error from anywhere within the
    Win32 API, regardless of the failure or sucess of the actual call that
    initiated the procedure. My guess (and this is a complete guess) is
    that Win32 internally makes several API calls for the copy command, one
    of which is to see if the target of the copy already exists. Because it
    does not, that call to that specific API procedure set the value of $^E.

    The "caveats" mentioned above boil down to: The error indicators (such
    as $! and $^E) are valid if and only if the immediately preceding system
    command failed. That is why you should always check the return value of
    the system call. If the system call failed, then and only then should
    you bother examining the error indicating variables.

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Aug 31, 2004
    #4
  5. Griff

    Joe Smith Guest

    Griff wrote:

    > system($cmdstr);
    > print "$^E";


    $^E is set if there is an error.
    $^E is *not* cleared on success; the previous (and now
    irrelevant) error message is still there.
    -Joe
     
    Joe Smith, Aug 31, 2004
    #5
  6. Griff

    Griff Guest

    Thanks everyone. In future I shall check the return value from system
    before going into the detail of $^E.
     
    Griff, Sep 1, 2004
    #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. Polo Lee
    Replies:
    0
    Views:
    2,767
    Polo Lee
    Jul 7, 2003
  2. Scott Lander

    Re: unexpected results

    Scott Lander, Jul 7, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,754
    Scott Lander
    Jul 7, 2003
  3. Elmar Krieger
    Replies:
    0
    Views:
    337
    Elmar Krieger
    Oct 26, 2003
  4. wdudek
    Replies:
    2
    Views:
    332
    Steven Cheng [MSFT]
    Jun 23, 2008
  5. John Nagle
    Replies:
    1
    Views:
    2,224
    John Nagle
    Feb 22, 2009
Loading...

Share This Page