Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing Slurped Data Fails

Status
Not open for further replies.

STufaro

Programmer
Joined
Jun 27, 2006
Messages
11
Location
US
Hi folks, I'm a bit new to Perl and still am working my way through it, so bear with me.

I want to generate webpages. (Of course!) All that changes is the middle of the page. I slurp in the top half of the page like so:

undef $/;
$file = "mptop.txt";
open INPUT, "$file";
$mptop = <INPUT>;
close INPUT; # slurped in top

And then the bottom half in a similar manner.

Then I go to print it:

# write in the top part of the HTML
print "document.write(\'$mptop\')\;";

But all I get is a blank page and the usual nondescript error in the SUEXEC log showing my script's name.

I perform no operations on the slurped in string. I'm merely copying it out of the file and into the webpage I'm generating (it's in a file because I don't want to deal with the HTML in the .pl file itself).

Any suggestions would be much appreciated, and thanks in advance!

- Steve.
 
there could be errors in the javascript,

try
Code:
print $mptop;
instead of
Code:
print "document.write(\'$mptop\')\;";
HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Yes, I've tried that already and still have a blank page. The JavaScript looks okay, though.

Could it be that I'm not getting it read into the variable correctly?
 
it could be so try this:

$file = 'mptop.txt';
open(INPUT, $file) or die "Can't open file '$file': $!";
$mptop = do {local $/; $file = <INPUT>};
close INPUT;

mptop.txt has to be in the same directory as the script or you have to change into the directory where mptop.txt resides in order to open the file using only the filename. Maybe you need to add a path:

$file = '/path/to/mptop.txt'
 
Update:

Oddly enough, when it can't find the file, it prints the rest of it (the middle of the page) correctly.

I have a bit of exploring to do.
 
Ah, that doesn't work either. Thanks for the help though, Kevin.

The textfile's been in the same directory (though I do tend to make a lot of path mistakes).
 
It shouldn't matter if I have double-quotes in the textfile, should it?

Just a thought. The reason I put it in the textfile is to avoid that and the qq junk.
 
Okay, I know no one's interested anymore. But I've gotten a step further.

It looks to be the file. When I replace the filename with another file in the directory, success. It fails otherwise.

The file is called "counter.txt" and contains only the text "91" with no carriage returns/line feeds.
 
Determined the cause of the problem but there's no visible way of fixing it.

It's because there are new line characters in the file. Perl is being quite the pesky little programming language.

I hear line-by-line xfer of text is faster, anyway, so I'm going to try reading it into an array.
 
Okay. Folks, I understand it now.

There are two problems, apparently, with the file I am using.

1) it is too long. (and it's only 496 bytes)
2) it contains new-line characters

I've tried both slurping it to a single variable and to an array. Both fail.

What am I to do?
 
the newlines and the file length should be no problem. What you are trying to do is a very common task so I am not sure why it's failing.
 
post what code you got at the minute ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Well, I've brute forced it by putting the HTML directly into the script.

But here's my code:

Code:
# Slurp top file
undef $/;
$file = "mptop.txt";
open INPUT, "<$file";
$mptop = <INPUT>;
close INPUT;
print $mptop;

And I can't imagine why it's not working. I'm wondering if it has to do with the bluehost server that I'm using, but their Perl version should be up-to-date and fully functional.

Anyway, as I said I fixed it by putting the raw HTML into the file. It would be nice to put the HTML in a separate file though to save time and to keep the script pretty (as well as not having to escape certain characters).

Thanks very much, guys.
- Steve.
 
Good news, actually.

I just copied this guy's (a one "Steve Litt") code from a tutorial and modified it slightly:

Code:
#!/usr/bin/perl -w
use strict;
open STDIN, "<testpage.txt";
my $hold = $/;
undef $/;
my $buf = <STDIN>;
$/ = $hold;
print "Content-Type: text/html

";
print $buf;
print "\n";

He's a Steve, I'm a Steve, he shouldn't mind.

And so that's the full code - that's what works.

It's essentially the same thing I had except with the my's (because of "use strict;" I assume?), the newline at the end, preserving the $/, and -w which appears to be a command line argument (?).

I'm glad I have something working now.
Thanks for your help guys, I'm sure I'll be back with more questions. I'll stay out of your way for now. :)

- Steve.
 
How is that different from:
Code:
open STDIN, "<testpage.txt";
print "Content-Type: text/html\n\n"
print while(<STDIN>);
print "\n";

I'm not a CGI writer, so I'm asking the gurus this question.

 
In terms of output, it's basically not different at all.

Correct me if I'm wrong:

You described a program which stores no text in a variable but instead prints as it reads it.

When Perl reaches the end of the file, <STDIN> returns undefined and you don't print anymore. Then you print the newline character.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top