[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Read entire file?



At 12:53 -0500 2000.12.27, Ronald J Kimball wrote:
>On Wed, Dec 27, 2000 at 11:00:48AM -0500, Jim Correia wrote:
>> So I'm a c programmer, and my perl book is at the office.
>>
>> What is the best (fastest, clearest code) to read an entire file into a
>> variable?  I want to preserve whatever line endings were in the original
>> file so I don't want to read it by lines.  Right now I am reading by
>> chunks because I can't seem to find a function that will return the size
>> in bytes of the file on disk...
>
>The -s filetest operator returns the size of the file.  I think Win32 is
>actually the only platform that changes the line endings, though.

Well, any DOS-ish platform will, but this usually means Win32.  Note that
-s does NOT return the actual size of a Mac file; it only returns the data
fork size (which is almost always what you want, though, so this is a Good
Thing).

Anyway, similarly to one or more of Ken's examples, this is my idiom of choice:

	my $text;
	{	local(FH, $/);
		open FH, "< $file" or die $!;
		$text = <FH>;
	}

-- 
Chris Nandor                      pudge@pobox.com    http://pudge.net/
Open Source Development Network    pudge@osdn.com     http://osdn.com/

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org