實作 fcntl(2) 函式。您可能必須先說
use Fcntl;
才能取得正確的常數定義。引數處理和傳回值的工作方式就像下方的 ioctl
。例如
use Fcntl;
my $flags = fcntl($filehandle, F_GETFL, 0)
or die "Can't fcntl F_GETFL: $!";
您不必檢查 defined
從 fcntl
傳回。就像 ioctl
,它會將系統呼叫傳回的 0
對應到 Perl 中的 "0 but true"
。這個字串在布林文中為真,在數字文中為 0
。它也免於正常的 Argument "..." isn't numeric
警告,因為不適當的數字轉換。
請注意,如果 fcntl
用於未實作 fcntl(2) 的機器上,它會引發例外。請參閱 Fcntl 模組或您的 fcntl(2) 手冊頁,以了解您的系統上有哪些可用的函式。
以下是一個範例,說明如何將名為 $REMOTE
的檔案控制代碼設定為在系統層級為非封鎖。不過,您必須自行協調 $|
。
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
my $flags = fcntl($REMOTE, F_GETFL, 0)
or die "Can't get flags for the socket: $!\n";
fcntl($REMOTE, F_SETFL, $flags | O_NONBLOCK)
or die "Can't set flags for the socket: $!\n";
可攜性問題:perlport 中的「fcntl」。