My sysopen fails:
sysopen(DEV, "/dev/ttyS0", O_NONBLOCK|O_RDONLY)
returns 1, which is a success! Then, why does $! have the error “Illegal seek” in it (where it is undef before the call)
before sysopen $!:
after sysopen $!: Illegal seek
EDIT:
Here is the full script: (using the actual O_NONBLOCK|O_RDONLY value 2048)
#!/usr/bin/perl -w
use Device::SerialPort;
my $ob;
$ob = new Device::SerialPort("/dev/ttyS0");
print $!, "\n";
$! = 0;
my $ret = sysopen(DEV, "/dev/ttyS0", 2048);
print $!, "\n";
$! = 0;
print "ret from sysopen: ", $ret, "\n";
#my $dev = <DEV>;
which prints out:
./filehandle.pl
Illegal seek
Illegal seek
ret from sysopen: 1
That’s how the C
errnovariable works. To quote fromman errno:$!is just Perl’s interface toerrno, and has the same behavior:You must check the return value of
sysopento determine whether it failed. If it failed,$!will tell you why it failed, but you can’t use$!to determine whether it failed (unless the particular function you’re using is documented to set$!to 0 on success. Most don’t, includingsysopen).