executing a command in perl

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

executing a command in perl

Post 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?
Only Human
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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:
Every good solution is obvious once you've found it.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post 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?
Only Human
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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().
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

perl is a WORN language - Write Once Read Never

What's the reasoning behind that open statement?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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. ;)
Every good solution is obvious once you've found it.
Post Reply