betterworld has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
as I was playing with the syscall() function, I discovered a strange behaviour when using IO handles as parameters.
This script prints:#!/usr/bin/perl use strict; use warnings; require 'syscall.ph'; open my $f, '</dev/null' or die "open: $!"; syscall(&SYS_write, 2, $f, 10) != -1 or warn "write: $!"; print STDERR "\n"; defined read($f, my $waste, 1) or die "read: $!";
GLOB(0x814 read() on unopened filehandle at ./script line 9. read: Bad file descriptor at ./script line 9.
Huh, perl sais that $f is an unopened filehandle, even though we opened it 3 lines ealier. Further inspection shows that the error messages will disappear if you delete the line containing syscall. Sure enough, stracing the script reveals that perl closes the file descriptor.
The IO reference $f is treated as a string and perl passes the memory address of that string to the write system call. I can't understand why that should close $f.
Is there an explanation to this or is it a bug?
(I know that the script would be pretty useless even if it "worked". I don't intend to pass IO references to syscall() in any real code. My question is just out of curiosity. Actually I discovered this when I forgot to use fileno() when passing a file descriptor to the fchmod syscall.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: syscall() closes file descriptor
by starbolin (Hermit) on Aug 22, 2006 at 21:23 UTC | |
by starbolin (Hermit) on Aug 22, 2006 at 21:47 UTC | |
by betterworld (Curate) on Aug 22, 2006 at 23:50 UTC | |
by starbolin (Hermit) on Aug 23, 2006 at 03:05 UTC | |
by starbolin (Hermit) on Aug 23, 2006 at 04:14 UTC | |
|
Re: syscall() closes file descriptor
by ikegami (Patriarch) on Aug 22, 2006 at 18:39 UTC |