How to break the lines in printing to html lines?

C

Ciba LO

Hello,

The following script fails to break the lines in listing the directory
contents with long listing format --- see below.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<br>";
$out = exec("ls -l");
print "$out<br>\n";
print "<br>";

And the browser displays as:
total 24 -rwx------ 1 apache apache 132 Jul 6 11:04 test.cgi -rwx------
1 apache apache 134 Jul 6 11:02 test.cgi~ -rwxrwxrwx 1 guest guest 253
Jul 6 10:48 test_a.cgi -rwxrwxrwx 1 guest guest 257 Jul 6 10:47
test_a.cgi~ -rwxrwxrwx 1 guest guest 511 Mar 15 21:18 test_b.cgi
-rwxrwxrwx 1 guest guest 511 Mar 15 21:17 test_c.cgi

Please help me to break the lines in printing to html lines!

Thank you very much in advance!!!

Ciba
 
J

John W. Krahn

Ciba said:
The following script fails to break the lines in listing the directory
contents with long listing format --- see below.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<br>";
$out = exec("ls -l");

The exec() function doesn't return what you seem to think it does.
print "$out<br>\n";

$out doesn't contain "lines" (in the HTML sense) it contains one string. Read
the documentation for the qx operator in the perlop manual:

perldoc perlop
print "<br>";


#!/usr/bin/perl
use warnings;
use strict;

print "Content-type: text/html\n\n",
'<br>',
map( "$_<br>\n", qx[ls -l] ),
'<br>';



John
 
S

Sherm Pendley

Ciba LO said:
The following script fails to break the lines in listing the directory
contents with long listing format --- see below.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<br>";
$out = exec("ls -l");
print "$out<br>\n";
print "<br>";

Exec() does not do what you think it does - the two print statements after
exec() in the above are never executed, and $out does not have the output
of the ls command.

See "perldoc -f exec" for details.
Please help me to break the lines in printing to html lines!

First sort out the problem with the exec() above.

After that, you have two options. One, you could split $out into lines, and
print a <br> after each line like you're doing above. Or, you could replace
all the \n's in $out with <br> in one go.

The first approach would use split() - see "perldoc -f split". The second
would use regular expressions - see "perldoc perlre".

sherm--
 
T

Tad McClellan

Ciba LO said:
The following script fails to break the lines in listing the directory
contents with long listing format --- see below.

$out = exec("ls -l");


Where does the output from ls go?

It goes to STDOUT.

In a CGI environment STDOUT ends up at the browser.

There are no <br>s in the output from ls, so there are no <br>
tags for the browser to act on.

print "$out<br>\n";


If the ls executes, then this print statement is _never_ reached!

(Because your perl process has been replaced by the ls process.
That's what the *and never returns* means in the first line
of the description for the exec function that you are using.
)

Please help me to break the lines in printing to html lines!


foreach my $line ( `ls -l` ) {
print "$line<br>";
}
 
C

Ciba LO

Hello John, sherm, Tad and Tim!

Many thanks to you guys for replying to my post.

Your suggestions are working okayed to me. And they are exactly what I
want them to be.

Ciba
Ciba said:
The following script fails to break the lines in listing the directory
contents with long listing format --- see below.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<br>";
$out = exec("ls -l");

The exec() function doesn't return what you seem to think it does.
print "$out<br>\n";

$out doesn't contain "lines" (in the HTML sense) it contains one string. Read
the documentation for the qx operator in the perlop manual:

perldoc perlop
print "<br>";


#!/usr/bin/perl
use warnings;
use strict;

print "Content-type: text/html\n\n",
'<br>',
map( "$_<br>\n", qx[ls -l] ),
'<br>';



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top