Easiest way to open a text file and read it into an array with Perl

前端 未结 8 1663
星月不相逢
星月不相逢 2020-12-14 02:46

Adding a standard Perl file open function to each script I have is a bit annoying:

sub openfile{
    (my $filename) = @_;
    open FILE,\"$filename\" or die          


        
8条回答
  •  没有蜡笔的小新
    2020-12-14 03:09

    I would recommend an object oriented approach that does not requires modules outside the CORE distribution and will work anywhere:

    use strict;
    use warnings;
    use IO::File;
    my $fh = IO::File->new("< $file");
    
    foreach ($fh->getlines) {
        do_something($_);
    }
    $fh->close
    

提交回复
热议问题