correia@barebones.com (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... You probably don't need to know that. You can slurp the file without it. Some examples: #1, perhaps the easiest open FH, 'file.txt' or die $!; my $var = join '', <FH>; close FH; #2, a little faster than #1 open FH, 'file.txt' or die $!; local $/ = undef; # See perlvar.pod my $var = <FH>; close FH; #3, a little cleaner scoping than #2 open FH, 'file.txt' or die $!; my $var = do {local $/; <FH>}; close FH; #4, cleaner scoping yet, and all on one line my $var = do {local(*FH,$/); open FH, 'file.txt' or die $!; <FH>}; I usually do something like #4 for quick scripts. For more involved stuff I'm probably not slurping files like this anyway. If you really want to use read(), you could do that too (but someone else will have to supply the examples). Check out the -s file test operator in perlfunc.pod. ------------------- ------------------- Ken Williams Last Bastion of Euclidity ken@forum.swarthmore.edu The Math Forum # ===== Want to unsubscribe from this list? # ===== Send mail with body "unsubscribe" to macperl-request@macperl.org