By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,835 Members | 2,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,835 IT Pros & Developers. It's quick & easy.

Close EXE & die command

P: 1
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.
Jan 5 '10 #1
Share this Question
Share on Google+
5 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jan 6 '10 #2

Expert Mod 100+
P: 589
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).
Expand|Select|Wrap|Line Numbers
  1. [root@fc4dev ~]# cat test-exit.pl
  2. #!/usr/bin/perl
  3.  
  4. exit 5;
Expand|Select|Wrap|Line Numbers
  1. [root@fc4dev ~]# cat test.pl
  2. #!/usr/bin/perl
  3.  
  4. use strict;
  5. use warnings;
  6.  
  7. my $exit_val = system("./test-exit.pl");
  8. print $exit_val >> 8, $/;
  9.  
Expand|Select|Wrap|Line Numbers
  1. [root@fc4dev ~]# ./test.pl
  2. 5
  3.  
Jan 6 '10 #3

Expert Mod 100+
P: 589
Instead of using a system call, it would be better to use Win32::Process
http://search.cpan.org/~jdb/Win32-Pr....14/Process.pm
Jan 6 '10 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
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.
Jan 6 '10 #5

Expert Mod 100+
P: 589
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
Jan 6 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.