Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Passing commands to subroutines

by repellent (Priest)
on Jul 03, 2009 at 22:57 UTC ( [id://777136]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Passing commands to subroutines
in thread Passing commands to subroutines

Multiple-arg system() doesn't quite work like that (read this). Redirection using the shell metacharacter ">" is handled by the shell, which reads the entire string "program $ref_file > $outfile", parses it, and executes the command with redirection.

When you do multiple-arg system, you're going raw and skipping the shell (usually). Redirections have to be performed manually and you'll lose some convenience you get with using a shell. Example:
sub executeComm { my ($outfile, @comm) = @_; print "cmd: <", join("> <" => @comm), ">\n"; # manual redirection - dup(2) STDOUT first open(my $ORIGSTDOUT, ">&" . fileno(*STDOUT)) or die $!; open(*STDOUT, ">", $outfile) or die $!; # run it! my $exit = system @comm; # restore STDOUT open(*STDOUT, ">&=" . fileno($ORIGSTDOUT)) or die $!; print "exit: ", $exit, "\n"; } print "before\n"; executeComm($outfile, $program, $ref_file); print "after\n";

If you really want a quick-and-dirty fix for your "whitespace-in-filename" problem, place single-quotes around the filenames, as in:
executeComm ("program '$ref_file' > '$outfile'");

Now make sure you don't have single-quotes in the filenames...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://777136]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-20 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found