in reply to Re^2: Handling the output of a system command -- how to best manage?
in thread Handling the output of a system command -- how to best manage?
Why did you change my line:
my @l = `ls -1b`;to:
my @l = system(`ls -1b`); ? Mine runs the ls command in a subshell and returns the results to the @l array, one line per array element. Yours runs ls in a subshell and captures the output, but then passes that output to system, which tries to run that in another sub-process, passing the return value of that sub-process to @l, making it the first (and only) element. Odds are very good that a listing of the files in your directory doesn't make a valid system command (and could possibly make a harmful one), so the return value is an error number, in this case, -1, 'failed to execute'.See perlipc and other docs on the difference between system and backticks, as well as other ways to run a sub-process and capture the output, like opening a pipe:
open my $pipe, '-|', 'ls -1b' or die $!; while(<$pipe>){ chomp; say qq| <tag attr='value'>$_</tag> |; } close $pipe;
There are a lot of different ways to do this. Perl isn't called a glue language for nothing. But some are usually better than others, depending on the circumstances.
Aaron B.
Available for small or large Perl jobs; see my home node.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Handling the output of a system command -- how to best manage?
by taint (Chaplain) on Jun 21, 2012 at 04:23 UTC | |
by aaron_baugher (Curate) on Jun 21, 2012 at 15:22 UTC |