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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.