I've been googling for a while, but I cannot find a function the read just first line of a file.
I need to read first line of a text file and extract the date from it.
new to perl.
open THEFILE, "<filename.txt";
$first_line = <THEFILE>;
close THEFILE;
... a modern and popular alternative:
use Path::Tiny;
(my $firstline) = path('filename.txt')->lines( { count => 1 } );
For more info https://metacpan.org/pod/Path::Tiny#lines-lines_raw-lines_utf8
Note: since ->lines
is returning a list, calling it without the brackets around $firstline
it will be assigned the number of lines which have been read from filename.txt
: 1 (or 0 if it's empty).
chomp $firstline;
or the value could have a newline at the end and that might not be something you want.