Skip to content
Advertisement

open command in perl

I am try to use open command in perl. But getting and error as follows.

Command

open(IN_TRIALS, '<', "F:/2010_nist_sre_test_set/data/eval/keys/coreext-coreext.trialkey.csv") or die "cannot open trials list";

Error cannot open trials.

how to fix this?

Advertisement

Answer

First, make sure the file exists in the location: F:/2010_nist_sre_test_set/data/eval/keys/coreext-coreext.trialkey.csv.

Use a modern way to read a file, using a lexical filehandle:

#!/usr/bin/perl

use strict;
use warnings;

my $filename = "F:/2010_nist_sre_test_set/data/eval/keys/coreext-coreext.trialkey.csv";
open(my $fh, '<:encoding(UTF-8)', $filename)
  or die "Could not open file '$filename' $!";
...
...
close $fh;

If you’re reading a csv file then it is recommended to use Perl Module Text::CSV_XS.

If you use Text::CSV_XS, here is the syntax:

use Text::CSV_XS;
 
my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 });

my $filename = "F:/2010_nist_sre_test_set/data/eval/keys/coreext-coreext.trialkey.csv";
open my $fh, "<:encoding(utf8)", $filename or die "$filename: $!";
while (my $row = $csv->getline ($fh)) {
    #do the necessary operation
}
close $fh;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement