trouble reading line by line

W

wmconlon

Really perplexed about reading a file line by line in a cgi script.

With one cgi parameter a subroutine does:

sub view {
my $file = $_[0];
open(TEXT, "<$file") or die "Can't open $file for preview";
while (defined ($text = <TEXT>)) {
....

This processes line-by-line.

Another cgi parameter invokes a different subroutine call which does:

sub add {
my $template_path=$_[0];
my $directory_path=$_[1];
my $name=$_[2];
open(TEXT, "<$template_path") or die "Can't open $template_path for
reading"; # open the template file
while( defined ($text = <TEXT>)) {
....
and reads the entire file into a single line, even though it is reading
the same file.

I guess it has something to do with scalar context, isn't
$template_path a scalar? I've battled this for a day, so it must be
something obvious.
 
G

Gunnar Hjalmarsson

wmconlon said:
Really perplexed about reading a file line by line in a cgi script.

With one cgi parameter a subroutine does:

sub view {
my $file = $_[0];
open(TEXT, "<$file") or die "Can't open $file for preview";
while (defined ($text = <TEXT>)) {
...

This processes line-by-line.

Another cgi parameter invokes a different subroutine call which does:

sub add {
my $template_path=$_[0];
my $directory_path=$_[1];
my $name=$_[2];
open(TEXT, "<$template_path") or die "Can't open $template_path for
reading"; # open the template file
while( defined ($text = <TEXT>)) {
...
and reads the entire file into a single line, even though it is reading
the same file.

Please post a short but *complete* program that demonstrates the
behaviour you mention.
 
T

teodell

That should work fine. The only thing is the $template_path an actually
file then? The section with $template_path, $directory_path and $name
sounds like template_path is a directory. IF it is a directory it will
slurp it up in one line since directories need opendir, readdir, etc.

todd
 
T

teodell

That should work fine. The only thing is the $template_path an actually
file then? The section with $template_path, $directory_path and $name
sounds like template_path is a directory. IF it is a directory it will
slurp it up in one line since directories need opendir, readdir, etc.

todd
 
T

teodell

That should work fine. The only thing is the $template_path an actually
file then? The section with $template_path, $directory_path and $name
sounds like template_path is a directory. IF it is a directory it will
slurp it up in one line since directories need opendir, readdir, etc.

todd
 
P

peter pilsl

sub view {
my $file = $_[0];
open(TEXT, "<$file") or die "Can't open $file for preview";
while (defined ($text = <TEXT>)) {
...


sub add {
my $template_path=$_[0];
open(TEXT, "<$template_path") or die "Can't open $template_path for
reading"; # open the template file
while( defined ($text = <TEXT>)) {
...

For what you showed us, the two subs does exactely the same. Looks like
the read files are different or $/ is changed between the calls.

best,
peter
 
W

wmconlon

$template_path contains an absolute file path. So it is a file, and
I've printed it to be sure. And I can print the contents of $text to
see it as a single line in one case, and the entire file in another.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top