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

    Greetings, and thank you for taking the time to help me on this.
    EUREKA!
    I maintained the previous revisions I made to your original reply. But,
    removed the system() call -- retuning it to `ls -1b`;

    It appears that the only issue I had with yours originally, was the single quote marks used in the HTML
    formatting -- ' vs ".
    For clarity (and anyone else that might be interested):

    #!/usr/bin/perl print "Content-type: text/html\n\n"; use strict; use diagnostics -verbose; # Desperately seeking guidance :P use Modern::Perl; my @l = `ls -1b`; my $c = 0; # toggle for odd/even lines for (@l){ chomp; my $bg = $c ? "#eee" : "#ccc"; say qq| <p style="font-family:mono;background:$bg">$_</p> |; $c = 0 if $c++ == 1; } # use strict; with use Modern::Perl; is redundant # keeping use strict; and commenting use Modern Perl; # would require changing "say" to "print"

    Thank you VERY much. The output is beautiful! :)

    #!/usr/bin/perl -Tw
    use perl::always;
    my $perl_version = "5.12.4";
    print $perl_version;

      You're welcome! I use single quotes around HTML attribute values all the time, so I'm not sure why that would have caused a problem, but I'm glad you got it working.

      Aaron B.
      Available for small or large Perl jobs; see my home node.