Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I need to execute a command line utility from Perl and get the windows return code for that particular execution Tried the following code System ("del temp.txt"); System ("echo %errorlevel"); But It always returns a Code 0 even if the file is not present (if should have returned 1) Can any help me in running more than 1 command using a single System command or is there ny way I can get this working.... Thanks in advance

Replies are listed 'Best First'.
Re: Perl System command
by maa (Pilgrim) on Jan 07, 2004 at 14:35 UTC
    Hi,...

    I'm assuming you're using Windows... DEL is part of CMD.EXE (or Command.COM) not a program in its own right... perhaps you should look at unlink (perldoc -f unlink) instead?

    HTH - Mark

    Update:

    Your system("echo %errorlevel%") call is spawned in a different CMD.EXE than your system("del tmp.txt"); so even if it worked it wouldn't be what you wanted... If you need to know if the file was there first you can do

    #simplest format if (-e $filename){ if ((unlink($filename)) { print STDERR "File $filename deleted.\n"; }else{ print SDTERR "Couldn't delete $filename $!\n"; } }else{ print STDERR "File didn't exist\n"; }
Re: Perl System command
by ColtsFoot (Chaplain) on Jan 07, 2004 at 14:21 UTC
    You could try something like
    my $error = system('del temp.txt'); print qq(Return value is $error\n);
    HTH
      Add a
      $error >>= 8;
      between your first and last line to get a more accurate value.

      Abigail

      Thanks for the quick reply. We r having a small problem with the return codes, the Return Code displayed is a multiple of "Actual Code * 256" Ex:- If the file is not present the expected code is 1 and getting return code as 256.
Re: Perl System command
by b10m (Vicar) on Jan 07, 2004 at 14:38 UTC

    Maybe this is redundant, but in your example, you don't need the system command at all. You could use unlink.

    Update: it is redundant now, since maa beat me in typing speed ;)

    --
    b10m
Re: Perl System command
by helgi (Hermit) on Jan 07, 2004 at 16:11 UTC
    my $cmd = "del temp.txt"; system ($cmd) == 0 or die "Cannot $cmd:$?\n";

    --
    Regards,
    Helgi Briem
    hbriem AT simnet DOT is