[00:28][nick:~/monks]$ cat 1139778.txt
foo
bar
baz
qux
####
#!/usr/bin/perl
use strict;
use warnings;
# Don't hard-code filenames in logic code
my $file = '1139778.txt';
# Use the three-argument form of open() ...
open( my $MYFILE, '<', $file )
or die "Failed to open $file: $!\n"; # ... and always check for success
# Use parentheses to show we want the whole list of lines
my $data = join '', (<$MYFILE>);
print "Using \$data:\n$data\n\n";
# Go back to the beginning of the file
seek $MYFILE, 0, 0;
# No parentheses so we get one line at a time
print "Using \$line:\n";
while ( my $line = <$MYFILE> ) {
# chomp; # No need to chomp if you are printing the file out again
print $line;
}
close $MYFILE or die "Failed to close $file: $!\n";
__END__
####
[00:29][nick:~/monks]$ perl 1139778.pl
Using $data:
foo
bar
baz
qux
Using $line:
foo
bar
baz
qux