Why Perl sysopen reports success, but $! is there a mistake?

My sysopen does not work:

sysopen(DEV, "/dev/ttyS0", O_NONBLOCK|O_RDONLY)

returns 1, which is successful! Then why $! there is an “Illegal search” error in it (where it is not defined before the call)

before sysopen $!:
after sysopen $!: Illegal seek

EDIT: Here is the full script: (using the actual value of O_NONBLOCK | O_RDONLY 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:. /filehandle.pl Illegal search Illegal search ret from sysopen: 1

+3
source share
2 answers

How the variable C works errno. Quote from man errno:

, ( -1 ; -1 NULL ); , errno.

$! - Perl errno :

, $!

sysopen, , . , $! , , $!, , ( , , , $! 0. sysopen).

+16

Perl , . , . - .

 unless( sysopen( ... ) ) {
      die "Error was $!";
      }

 if( m/(...)(...)/ ) {
      print "Found $1 and $2\n";
      }

, , Perl, , , . , - , , .

+1

Source: https://habr.com/ru/post/1768667/


All Articles