Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Printing From a File Handle

by haoess (Curate)
on Apr 28, 2004 at 20:22 UTC ( [id://348935]=note: print w/replies, xml ) Need Help??


in reply to Printing From a File Handle

@fileinput_ap = <AP_MENU>; for ($i = 0; $i <= $#fileinput_ap; $i++) { print "$fileinput_ap[$i]" +}

This can be done more perlish:

while( <AP_MENU> ) { print; }
or shorter:
print while <AP_MENU>;

If you just want to print an entire file, you can use print in array context

print <AP_MENU>;
-- Frank

Replies are listed 'Best First'.
Re: Re: Printing From a File Handle
by bkiahg (Pilgrim) on Apr 28, 2004 at 20:27 UTC
    Is there anyway to add html formatting to it? e.g.
    print while "<AP_MENU><br>";
      TIMTOWTDI:
      # shortest print "$_<br />" while <AP_MENU>; # show what you mean { local $\ = "<br />"; print while <AP_MENU>; } # more readable? foreach my $line (<FILE>) { print $line, "<br />"; }
      -- Frank

      Still more ways to do it:

      # list processing print map "$_<br />", <AP_MENU>; # more abuse of special variables { local $, = "<br />"; print <AP_MENU>, ''; }

      If you are reading in any old file for display in an HTML page, you will want to do at least minimal escaping of special characters, too:

      while ( <AP_MENU> ) { # protect against most egregious HTML violations s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; # remove trailing whitespace so the <br /> is on # the same line as main text s/\s+$//; print "$_<br />\n"; }
        Good call tkil I forgot about escaping html characters!
      #!/usr/bin/perl print "Content-type: text/html\n\n"; print <<top; <html> <head><title>Some Title</title></head> <body bgcolor="#000000"> top print "<font color=\"#FFFFFF\">$_</font><br>" for <DATA>; print <<bottom; </body> </html> bottom __DATA__ Test Test Again One More Test!
      You can see it work here.

      www.perlskripts.com
Re: Re: Printing From a File Handle
by bkiahg (Pilgrim) on Apr 28, 2004 at 20:39 UTC
    while ( <LOG> ) { print "$_<br>" } does the trick.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://348935]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found