Dear Monks,

as I was playing with the syscall() function, I discovered a strange behaviour when using IO handles as parameters.

#!/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: $!";
This script prints:
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.)


In reply to syscall() closes file descriptor by betterworld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.