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>; }
In reply to Re^2: Determine whether file is dos or unix format
by ikegami
in thread Determine whether file is dos or unix format
by fritzvtb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |