Close EXE & die command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbhandari
    New Member
    • Jan 2010
    • 1

    Close EXE & die command

    Hi,

    I have following line in my Perl code-->

    system("start my.exe myfile.ext") == 0 or die "error ($?) : $!";

    Q1: How do I close my.exe?
    Q2: If I give myfile.ex1 instead of myfile.ext, myfile fails to open.
    However, the error code returned by Perl is still 0.
    Normally (when I specify myfile.ext), if I check what is stored in $!, its always
    "No such file or directory".
    I want to report the error while opening 'myfile'. How can it be done?

    Thanks.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    If you read the description of the system() funtion, it only returns a 0 or 1 (success or failure). There isn't anything else to get. You can try using backtics instead of the system command, to execute your exe file. I think you can then capture an exit code from the program.

    As for killing it, you are on a Windows system (derived from the fact that you are executing an exe file) so I have no idea how you would do that. If it were a Unix system I could help you.

    The "No such file or directory" is a valid error and exactly what you would get when trying to open a file that doesn't exist. You specified a wrong name and that's the error you get.

    Regards,

    Jeff

    Comment

    • RonB
      Recognized Expert Contributor
      • Jun 2009
      • 589

      #3
      If you read the description of the system() funtion, it only returns a 0 or 1 (success or failure). There isn't anything else to get.
      Sorry Jeff, but that is not correct.

      The return value is the exit status of the program as returned
      by the "wait" call. To get the actual exit value shift right
      by eight (see below).
      Code:
      [root@fc4dev ~]# cat test-exit.pl
      #!/usr/bin/perl
      
      exit 5;
      Code:
      [root@fc4dev ~]# cat test.pl
      #!/usr/bin/perl
      
      use strict;
      use warnings;
      
      my $exit_val = system("./test-exit.pl");
      print $exit_val >> 8, $/;
      Code:
      [root@fc4dev ~]# ./test.pl
      5

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Instead of using a system call, it would be better to use Win32::Process

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Touche! See, I learned something new today. I always knew it as a 0 or 1. Must play with that one.

          Must say though, I never use it, I always use back tics.

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            Using backticks is a very common approach, if you need to capture the stdout of the command. If not, then it's the wrong choice.

            My suggestion of using Win32::Process was for 2 reasons; 1) the OP is on Windows and 2) it provides an easy method to kill the process if needed.

            A better and platform independent approach would be to use one of the IPC modules, such as IPC::Run IPC::Open2 or IPC::Open3

            Comment

            Working...