Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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

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>";
      # 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.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://348935]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2018-03-17 21:29 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (226 votes). Check out past polls.