sub getfile { my $filename = shift; open F, "< $filename" or die "open failed: $!"; my $contents = ''; while (<F>) { $contents .= $_; } close F; return $contents; }
This is inefficient, because the <F> operator makes Perl go to all the trouble of breaking the file into lines and returning them one at a time, and then all we do is put them back together again. It's cheaper to read the file all at once, without all the splitting and reassembling. (Some people call this slurping the file.) Perl has a special feature to support this: If the $/ variable is undefined, the <...> operator will read the entire file all at once: ## Below is better pratice ## sub getfile { my $filename = shift; open F, "< $filename" or die "open failed $!" my $contents; { local $/ = undef; # Read entire file at once $contents = <F>; # Return file as one line } close F; return $contents; } I read an excerpt from http://perl.plover.com/local.html#2_Localized_Filehandles and was wondering if this holds true regarding localizing $/ and setting it to undef in Perl 5.8.x? Outside of a situation when localizing $/, is it best practice to set $/ to undef considering "If the $/ variable is undefined, the <...> operator will read the entire file all at once"? thx... derek __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>