Re: Read File Problem

2008-07-29 Thread Dr.Ruud
Rob Dixon schreef:

>   my $contents = do {
> open my $fh, $source_file or die $!;
> local $/;
> <$fh>;
>   };

That has the file's contents in memory twice. This is leaner:

  my $contents;
  {
open my $fh, $source_file or die $!;
local $/;
$contents = <$fh>;
  };

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Read File Problem

2008-07-23 Thread Gunnar Hjalmarsson

[EMAIL PROTECTED] wrote:

How can I get the read line to read the entire file?


That's a FAQ.

perldoc -q "entire file"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Read File Problem

2008-07-23 Thread Rob Dixon

[EMAIL PROTECTED] wrote:
> 
> I am trying to read a text file with carriage returns and line feeds on a 
> unix system and for some reason it will read only one of text. I use the same
> code in a different perl script and I can read a file that does not have
> carriage returns and line feeds.
> 
> The existing code is :
> 
> open (SOURCE_FILE,$source_file) or die;
> $_ =  
> How can I get the read line to read the entire file?

If you want the entire contents of the file in one scalar variables you should
write:

  my $contents = do {
open my $fh, $source_file or die $!;
local $/;
<$fh>;
  };

The line

  local $/;

resets the input record separator so that the file isn't split up into lines.
You should read non-text (binary) files this way as well, as there is a chance
that a binary file may have line-feed characters in it and you could read only
part of the file when you think you have it all.

It is often better to read and process a text file line by line, so that your
program doesn't take up large amounts of memory unnecessarily. Also, if your
file is small, it can be easier to handle if you read it into an array instead
of a scalar, with one record per array element. You should consider these
possibilities with your application.

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Read File Problem

2008-07-23 Thread andrewmchorney
Hello

I am trying to read a text file with carriage returns and line feeds on a unix 
system  and for some reason it will read only one of text. I use the same code 
in a different perl script and I can read a file that does not have carriage 
returns and line feeds.

The existing code is :

open (SOURCE_FILE,$source_file) or die;
$_ = http://learn.perl.org/