in reply to How to remove newline characters at the end of file
The first part of the trick is to 'slurp' the file. Set the special variable $/ to undef to read the entire file in one hit:
use strict; use warnings; my $file = <<FILE; 1 2 3 FILE open my $fIn, '<', \$file; my $str = do {local $/; <$fIn>}; close $fIn; print "1-->\n$str<--1\n"; $str =~ s/\n+\Z//; print "2-->\n$str<--2\n";
Prints:
1--> 1 2 3 <--1 2--> 1 2 3<--2
|
|---|