in reply to Handling the output of a system command -- how to best manage?

When you use system, the output of your command goes straight to STDOUT, which is sent to the browser. You can wrap it in <pre> tags or something similar to have it be displayed as-is, but that's about the most you can do with it. To get fancier formatting, you'll probably want to capture it so you can wrap the individual lines in something. In real life I'd use classes and put the CSS in a separate file, but this should give you the idea -- capture the output with backticks and process it line-by-line:

#!/usr/bin/env perl 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; }

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

Replies are listed 'Best First'.
Re^2: Handling the output of a system command -- how to best manage?
by taint (Chaplain) on Jun 20, 2012 at 23:46 UTC
    Greetings, and thank you for your reply.
    After adding
    print "Content-type: text/html\n\n";
    I attempted to use your example, however, it failed to produce any output (to the browser).
    Perl complained (rightfully) about
    ( Insecure $ENV{PATH} while running with -T switch )
    So modified it as follows:
    use Modern::Perl; my @l = system(`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; }
    Which produces one line with a grey background, that simply has a -1 in it.

    I'm confused. Any further thoughts?
    Thank you again for your response.



    use perl::always;
    my $perl_version( 5.12.4 );
    print $perl_version;

      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.

        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;