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.
| [reply] [d/l] [select] |
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; | [reply] [d/l] [select] |
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.
| [reply] [d/l] [select] |
use Cwd;
my $dir = getcwd;
Can you please elaborate on what you mean for the second part of your question? | [reply] [d/l] |
Greetings, and thank you very much for your reply.
Yes, I can elaborate. :)
Firstly, I'm not very good at this. But after many fails, this was the best I could conceive:
my $listing = $ENV{CWD};
While it did give me Current Working Directory.
Perl(1) was (rightfully) unhappy with my choice
( Insecure $ENV{PATH} while running with -T switch ).
Why do I care about CWD||PWD? Because my script will live in /cgi-bin/, or @INC.
I need to include it where ever I call it from
eg; <!--#exec cgi="/cgi-bin/filelist.pl" -->.
I've looked at other modules that would dump/list the contents of folders -- File::Listing, for example.
But given that none of them offered the ability to alternate td/div background colors for readability,
and that there are thousands of, ahem... PHP scripts that do this. I thought this might be a worthy endevour -- god knows I need the practice! :)
I hope this makes better sense of my delemna, and thank you again for your response.
#!/usr/bin/perl -Tw
use strict;
use perl::always;
my $perl_version( 5.12.4 );
print $perl_version;
| [reply] [d/l] [select] |