Browser Draing problem

They have: 23 posts

Joined: Jan 2001

I am new to perl so bear with me...

I wrote a CGI that loops and finds lines in a flat file and draws a table with the contents to the browser for each line. My questions is:Is it better to draw to a file or variable and then send the entire thing to the browser after the loop has completed. Or should the loop draw to the browser? I would post code but there is alot of it.

Thanks
G

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

why do things twice (or more...)
while looping thro- the flat file, just print it to the browser

They have: 16 posts

Joined: Jan 2001

Quote: Originally posted by Mark Hensler
why do things twice (or more...)
while looping thro- the flat file, just print it to the browser

Excellent advice, there are too many scripts that use something like this:

open (FILE, $file);
@filedata = ;
close FILE;

foreach (@filedata) {
$ToPrint .= $_;
}

print $ToPrint;

..which is fairly ludicrous, much better written as Mark suggested.

open FILE, $file or die $!;
flock FILE, 1; #if you like....
while () {
# dosomething with $_ if you need too
print $_
}
close FILE;

my $friends = qq[=:Jilly , Andrew , Peter & Harry:=];
print &welcome($friends), "2"; my @home;
sub welcome{my $s=shift; $s=~s{^(=\Smiling(.+?)(\:=)$}
{$_=$2;@home=split/[&,]/;}esgx;$friends= join'me',@home;
$friends=~s{(\A|\S|\s+)([A-Z]).+?(\s|\Z)}{$2}sge;my$c=-1;
$friends=~s{(me|\Z)}{++$c;@_=(qw|ust nother erl acker|);qq!$_[$c] !;}eg; $friends}

They have: 850 posts

Joined: Jul 1999

Here is my go at the simple task. It only prints once.

{
     local $/;
     open(IN,"<$file") or die "Cannot Open $file: $!";
     print <IN>;
     close(IN);
}
'

[Edited by Rob Pengelly on Jan. 24, 2001 at 04:26 PM]

They have: 23 posts

Joined: Jan 2001

Ok, that's how I am doing it. I just wanted to make sure I was going about it correctly.

Thanks All

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.