in reply to Re: Determine whether file is dos or unix format
in thread Determine whether file is dos or unix format
The snippet
open FILE, 'test_file.txt' || die "$!\n"; my $text = join( '', @{ [ <FILE> ] } ); close FILE;
It will never die because "||" has higher precedence than ",".
open FILE, 'test_file.txt' || die "$!\n";
means
open FILE, ('test_file.txt' || die "$!\n");
so use
open FILE, 'test_file.txt' or die "$!\n";
or
open(FILE, 'test_file.txt') || die "$!\n";
It is ineffecient you create an anonymous array and immediately dereference it.
join( '', @{ [ <FILE> ] } );
is equivalent to
join( '', <FILE> );
It is also ineffecient because join is slower and less memory efficient than undefining $/.
my $text = join( '', <FILE> );
is equivalent to the more efficient
my $text; { local $/; $text = <FILE>; }
So you end up with
my $text; { open(my $fh, '<', 'test_file.txt') or die("Unable to open input file: $!\n"); binmode($fh); local $/; $text = <$fh>; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Determine whether file is dos or unix format
by blazar (Canon) on Nov 29, 2005 at 08:16 UTC | |
by ikegami (Patriarch) on Nov 29, 2005 at 16:16 UTC |