PracticeDev/study_perl/read_file.pl

23 lines
480 B
Perl
Raw Normal View History

2022-12-20 17:31:11 +08:00
#!/usr/bin/perl
use strict;
use warnings;
use Path::Tiny;
use autodie; # die if problem reading or writing a file
my $dir = path("/tmp"); # /tmp
my $file = $dir->child("file.txt");
# Read in the entire contents of a file
my $content = $file->slurp_utf8();
# openr_utf8() returns an IO::File object to read from
# with a UTF-8 decoding layer
my $file_handle = $file->openr_utf8();
# Read in line at a time
while( my $line = $file_handle->getline() ) {
print $line;
}