Midnite has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm new here, and I have a question. I am working on a script that one of the components is reading in a html file and then printing it at the appropriate place. I did this fine in a test file, it is coded like this:
#!/usr/local/bin/perl -w use CGI qw(:standard :html3); #print "hello\n"; $locat = "../../htdocs/elghome/material_office.html"; print header, start_html; open (FILE1, $locat) || die ("Material_Office open failed: $!"); while (<FILE1>) { $page .= $_; } close(FILE1); print "$page\n"; print end_html;
Now I put most of that code into another working script, one of the many I want to use it on. And strange enough, it does not work. I have gone through varios tests to see why, but the only thing I know, is that for some reason it is not reading it in correctly. The code I use in the other script is:
$locat = "www.nsn.org/elghome/material_office2.htm"; open (HTMLP,$locat); while (<HTMLP>) { $pageht .= $_; } close(HTMLP);
Further down I use this code to print it out:
<hr> <p>$pageht <hr>
I do get the 2 lines but, with nothing inbetween. And it works in a script all by itself. If anyone has suggestions, I would be more than happy to try anything. Thanks, Midnite

Replies are listed 'Best First'.
(jeffa) Re: Printing a Html Page
by jeffa (Bishop) on Jul 23, 2002 at 16:56 UTC
    In your first example, you test that open succeeded with a die, but you did not do that in your second example. Add or die $! and see what happens. Also, you can replace the while loop with the succinct:
    my $pageht = do {local $/;<HTMLP>};
    and don't forget to use strict and warnings. You might want to also make sure that material_office.html is readable by the webserver (chmod 644).

    Update - ahh good catch dimmesdale! Yes, you cannot open a file like that:

    $locat = "www.nsn.org/elghome/material_office2.htm";
    If this file is on the same server as your script, open it directly, and do you mean .htm or html?
    my $locat = '/path/to/htdocs/elghome/material_office2.htm'; open FILE, $locat or die "can't read $locat: $!";
    If this file is on another server, use LWP.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Actually, It wouldn't work until I took out the die. I originally had it in, I am also using the CGI qw(html3: and standard:) I have two files both the saem, but for testing purposes I named a new one, and also took out some html code, the form tags. I think it is on the same server, but I will try strict, but I'm not sure about it because I'm using it in a script that has over 40 variables in it. Joe
Re: Printing a Html Page
by Ovid (Cardinal) on Jul 23, 2002 at 17:12 UTC

    You want to use something like LWP::Simple for this.

    #!/usr/local/bin/perl -wT use CGI qw(:standard :html3); use LWP::Simple; use strict; my $locat = "http://www.nsn.org/elghome/material_office2.html"; # note that there is no better diagnostic info from get() my $html = get( $locat ) or die "Could not read data from $locat"; print header, start_html, $html, end_html;

    However, using this version of get() means you will not be able to check the response codes or headers, if necessary. $html will simply be undef on failure. If you still have troubles, read the LWP::Simple docs for how resolve this issue (&get_print, for example, gives you response codes that you can check, but it clearly has the side-effect of directly printing the data).

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Printing a Html Page
by dimmesdale (Friar) on Jul 23, 2002 at 16:57 UTC
    I'm not sure that you can read in an external source via the system's 'open' (or perl's for that matter) function. You may want to see the LWP module (LWP module at CPAN, if the link didn't come out correctly).

    There are some examples of its use at TPJ, and on this site. I suggest the tutorial section, or the HTML docs at cpan.

    (I assume the URL is linking outside your machine... is that correct?)

Re: Printing a Html Page
by derby (Abbot) on Jul 23, 2002 at 16:57 UTC
    You might want to check the open (as you do in the initial snippet).

    -derby

RE: Printing a Html Page
by Midnite (Acolyte) on Jul 23, 2002 at 20:13 UTC
    I hope I can do this here, thank you for all the responses, the die did work, and my path to the html file on the server was wrong.

    I thank everyone for advice, I may have been looking at that code forever.

    Midnite

    Joseph A. Ruffino
    Automated Systems Assistant
    Gail Borden Public Library District
    200 N. Grove Ave, Elgin, Il, 60120
    847-742-2411 x367 jruffino@nsls.info

    Edited: ~Tue Jul 23 21:26:37 2002 (GMT), by footpad: Reparented to original thread and updated title accordingly.