Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

open a file and print out to webbrowser

by grashoper (Monk)
on Oct 30, 2007 at 19:08 UTC ( #648087=perlquestion: print w/ replies, xml ) Need Help??
grashoper has asked for the wisdom of the Perl Monks concerning the following question:

I can't figure why this isn't working, what am I doing wrong?
my $sitelist_file="<c:\supportweb\content\sitelist.txt" open DATA, $sitelist_file; my @lines=<DATA>; close DATA; for each my $line (@lines){ $response->write($line); }

Comment on open a file and print out to webbrowser
Download Code
Re: open a file and print out to webbrowser
by Sidhekin (Priest) on Oct 30, 2007 at 19:15 UTC

    Ah. PerlScript. :)

    One problem is with your filename string:

    my $sitelist_file="<c:\supportweb\content\sitelist.txt" print $sitelist_file; __END__ <c:supportwebntentsitelist.txt

    Double-quoted strings treat backslash as starting escape sequences. Use single quotes instead:

    my $sitelist_file='<c:\supportweb\content\sitelist.txt'; print $sitelist_file; __END__ <c:\supportweb\content\sitelist.txt

    A more fundamental problem is that you're not checking if your open succeeded. A standard check would have revealed this problem to you:

    open DATA, $sitelist_file or die "Cannot open '$sitelist_file': '$!'";

    ... that is, if you know where to find the error log. It's been too long since I looked at ASP and PerlScript.

    Oh, and you have a syntax error. Typo? for each should be foreach, or simply for:

    for my $line (@lines){ $response->write($line); }

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: open a file and print out to webbrowser
by throop (Chaplain) on Oct 30, 2007 at 23:11 UTC
    When I'm getting no output whatsoever to a webbrowser, the usual reason is that I've forgotten to print a header. Try
    use strict; use CGI::Pretty; use Carp qw(croak); my $sitelist_file='<c:\supportweb\content\sitelist.txt'; my $title = 'My Lines'; open my $data, '<', $sitelist_file or croak "No open on $sitelist_file"; my $text = do{local $/; <$data>}; close $data; print header(), start_html({title=>$title}); foreach my $line (split "\n", $text){ $response->write($line); }; print end_html;
    or even more compactly
    use strict; use CGI::Pretty; use Perl6::Slurp; print header(), start_html(-title=>'My Lines'); foreach (split "\n", slurp '<c:\supportweb\content\sitelist.txt'){ $response->write($_)} print end_html;
    Not tested
    I'm interpreting you to mean that you're writing a cgi-bin program or some such. Unlike static webpages (.html pages) they need to have the magic
    Content-Type: text/html; charset=ISO-8859-1
    or similar.

    throop

    hat-tip to Perl Best Practices

      I'm betting that's ASP with PerlScript, not CGI.

      Note the $response->write. Ah, nostalgia. :)

      print "Just another Perl ${\(trickster and hacker)},"
      The Sidhekin proves Sidhe did it!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://648087]
Approved by Sidhekin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-09-17 01:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls