Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   How to break the lines in printing to html lines? (http://www.velocityreviews.com/forums/t898867-how-to-break-the-lines-in-printing-to-html-lines.html)

Ciba LO 07-06-2006 03:21 AM

How to break the lines in printing to html lines?
 
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


John W. Krahn 07-06-2006 03:37 AM

Re: How to break the lines in printing to html lines?
 
Ciba LO wrote:
>
> 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
--
use Perl;
program
fulfillment

Sherm Pendley 07-06-2006 03:39 AM

Re: How to break the lines in printing to html lines?
 
"Ciba LO" <cibalo@gmx.co.uk> writes:

> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

Tad McClellan 07-06-2006 03:57 AM

Re: How to break the lines in printing to html lines?
 
Ciba LO <cibalo@gmx.co.uk> wrote:

> 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>";
}


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Ciba LO 07-08-2006 02:58 AM

Re: How to break the lines in printing to html lines?
 
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

John W. Krahn wrote:
> Ciba LO wrote:
> >
> > 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
> --
> use Perl;
> program
> fulfillment




All times are GMT. The time now is 02:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.