Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Printing From a File Handle

by haoess (Curate)
on Apr 28, 2004 at 20:22 UTC ( #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


Comment on Re: Printing From a File Handle
Select or Download Code
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
      #!/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

      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!
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
Node Status?
node history
Node Type: note [id://348935]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-12-29 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (193 votes), past polls