不适当的I/O控制操作

tai*_*iko 5 perl syntax-error

我的问题很简单.但无法解决执行我的Perl脚本后收到的"不适当的I/O控制操作"错误.

#!C:/Perl/bin/perl.exe -w
use strict;

my $file = "D:/file.csv";
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
    chomp $line; 
    my @fields = split "," , $line;
    print $fields[1]."\n";
}
Run Code Online (Sandbox Code Playgroud)

任何想法,我做错了什么?我在Windows7上的ActiveState perl上运行此脚本

Zai*_*aid 5

我怀疑你的脚本是$!通过open && die $!或打印的值open or die $!; print $!;.

这是一个在Windows上重现相同问题的最小脚本:

C:\> perl -e "open my $fh, '<', 'file_that_opens' && die $!"
Inappropriate I/O control operation
Run Code Online (Sandbox Code Playgroud)

以下是*nix上发生的事情:

$ perl -e 'open my $fh, "<", "file_that_opens" && die $!'
Inappropriate ioctl for device
Run Code Online (Sandbox Code Playgroud)

记录此行为

据说perldoc perlvar,$!只有在失败的情况下才有意义.当open被调用时,它设定了一个值$!,但如果该值才有用open没有成功:

...... $!只有在失败立即才有意义:

if (open my $fh, "<", $filename) {
              # Here $! is meaningless.
    ...
}
else {        # ONLY here is $! meaningful.
    ...       # Already here $! might be meaningless.
}
# Since here we might have either success or failure,
# $! is meaningless.
Run Code Online (Sandbox Code Playgroud)

这里,无意义意味着$!可能与open()运营商的结果无关.