Page 1 of 1

executing a command in perl

Posted: Tue Dec 05, 2006 6:54 am
by Neo
I am trying to use perl to unzip & untar a file using the system command.
Is there any way I can determine if this worked successfully?
I am using

Code: Select all

$result = system("gunzip $fileName");
but am not sure if there is a better way?

Posted: Tue Dec 05, 2006 7:21 am
by Solar
Uh... Perl's system() is not exactly broken, but not easy to handle either. Most importantly, what gets returned by system() is not the return code of the command executed:
  • -1 means the command failed to start, "inspect $! for the reason";
  • if the process terminated normally, the 8 low-order bits of the return value are 0, the 8 high-order bits contain the exit() value (i.e., the return code of the command executed);
  • if the process terminated due to a signal, the 8 high-order bits contain 0, and the 8 low-order bits contain the signal value;
  • if the process was stopped, the low-order bits contain WSTOPFLG, and the high-order bits contain the signal that caused the process to stop.
Have fun... :wink:

Posted: Tue Dec 05, 2006 9:14 pm
by Neo
Solar wrote:Uh... Perl's system() is not exactly broken, but not easy to handle either. Most importantly, what gets returned by system() is not the return code of the command executed:
Is there any alternative to executing commands from within perl?

Posted: Wed Dec 06, 2006 1:36 am
by Solar
Backticks?

Or you can do:

open( CMD, "foo |" )

to execute foo, and reading its output. AFAIK, after closing the pipe $? contains the same "return code" as system().

Posted: Thu Dec 07, 2006 11:39 am
by Candy
perl is a WORN language - Write Once Read Never

What's the reasoning behind that open statement?

Posted: Fri Dec 08, 2006 1:00 am
by Solar
No, absolutely correct. foo gets executed by the shell, output gets piped to the file handle.

You shouldn't use this style of file handle; there's something called "anonymous file handles" where you assign a file handle reference to a variable or somesuch... I don't use Perl except for quick&dirty, so I never got into the fine print much. ;)