#!/usr/bin/perl -w use Fcntl; # Experimenting with fcntl system call ## open this file open( FH, ">>fcntl.pl" ); $cur_opts = fcntl( FH, &F_GETFL, 0 ); # The print statement of death.... # print "cur_opts -> $cur_opts\n"; $rc = fcntl( FH, &F_SETFL, $cur_opts ); # "fixes" The print statement of death... #$rc = fcntl( FH, &F_SETFL, $cur_opts & 0xffffffff ); print "rc -> $rc\n"; $new_opts = fcntl( FH, &F_GETFL, 0 ); print "new_opts -> $new_opts\n"; if ( $new_opts != $cur_opts ) { print "ERROR\n"; } else { print "Calling F_SETFL with F_GETGL return value succeeded.\n"; } print "new_opts -> $new_opts cur_opts -> $cur_opts\n";
In trying to verify the proper use of fcntl (so that I can set a socket file descriptor to non-blocking), I stumbled upon the above curiosity. When I run the above code, the output generated is
rc -> 0 but true new_opts -> 1025 Calling F_SETFL with F_GETGL return value succeeded. new_opts -> 1025 cur_opts -> 1025
However, if I un-comment out the "print statement of death" line (see code above), I get the following output:
cur_opts -> 1025 rc -> 0 but true new_opts -> 8193 ERROR new_opts -> 8193 cur_opts -> 1025V@S@ÿÿÿÿ0ÿÿÿÿÿÿÿÿHÿÿÿÿ N@
What I want to know, then, is what crime against nature have I committed by printing out the value of $cur_opts. Or, more importantly, how can I avoid this type of problem in the future? Note that masking off the 32 low order bits in the subsequent F_SETFL call allowed me to include the print line of death. Thanks for your assistance/explanation,
Victor

Edit by tye, add CODE tags, remove BR


In reply to printing fcntl( fd, F_GETFL,0 ) return value changes its representation by weinstev

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.