G'day Nobby,
I see that the STDOUT/STDERR problem has been explained.
Here's some additional comments.
-
Always start your code with the strict and
warnings pragmata.
-
Let Perl do the I/O exception handling for you with the autodie pragma.
-
You'll need to escape characters that are special to HTML (e.g. change '&' to '&').
In the code below, I used HTML::Escape.
-
Use lexical variables for your filehandles.
Package variables have pretty much the same problems as global variables and should generally be avoided.
-
Use the 3-argument form of open().
For this particular exercise, see the
"Opening a filehandle into a command" section.
-
Aim to not leave filehandles open any longer than is absolutely necessary.
In the code below, see how I achieved this using an anonymous block.
-
Tip: With 'chomp $_; print "$_\n";' you've removed the newline in the first statement then added it back in the next.
The chomp and subsequent \n are both unnecessary.
As many Perl functions use $_ in the absence of any other expression, $_ is unnecessary in both statements.
You could have written that four-line (six-line if you include the two lines of pointless whitespace) while loop
as the one-line: 'print while <runmain>;';
compare with my "print escape_html($_) while <$cmd_pipe>;" (in the code below).
-
If you lay out your code with consistent indentation, it will be easier to read;
in addition, this will often highlight logic errors.
Here's some output from my command line:
$ ls -al a_real_dir
total 4
drwxr-xr-x 1 ken None 0 Nov 19 18:20 .
drwxr-xr-x 1 ken None 0 Nov 19 19:10 ..
-rw-r--r-- 1 ken None 0 Nov 19 18:20 demo_file_A
-rw-r--r-- 1 ken None 0 Nov 19 18:20 demo_file_B
$ ls -al not_a_real_dir
ls: cannot access 'not_a_real_dir': No such file or directory
Here's a demo script that uses all of the points I raised:
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use HTML::Escape 'escape_html';
my %commands = (ls => '/usr/bin/ls -al');
my @targets = qw{a_real_dir not_a_real_dir};
my $program = 'ls';
print "<html>\n<head>...</head>\n<body>\n";
for my $target (@targets) {
my $cmd = "$commands{$program} $target 2>&1";
print '<h1>', escape_html($cmd), "</h1>\n<pre>\n";
{
open my $cmd_pipe, '-|', $cmd;
print escape_html($_) while <$cmd_pipe>;
}
print "</pre>\n";
}
print "</body>\n</html>\n";
Output:
<html>
<head>...</head>
<body>
<h1>/usr/bin/ls -al a_real_dir 2>&1</h1>
<pre>
total 4
drwxr-xr-x 1 ken None 0 Nov 19 18:20 .
drwxr-xr-x 1 ken None 0 Nov 19 19:10 ..
-rw-r--r-- 1 ken None 0 Nov 19 18:20 demo_file_A
-rw-r--r-- 1 ken None 0 Nov 19 18:20 demo_file_B
</pre>
<h1>/usr/bin/ls -al not_a_real_dir 2>&1</h1>
<pre>
/usr/bin/ls: cannot access 'not_a_real_dir': No such file or d
+irectory
</pre>
</body>
</html>
"I have omitted a lot of the header and html code, ..."
That's fine. My code is only intended as a demo example: adapt to your needs.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.