This will return the open mode for a given Perl filehandle as a number.
- fmode($fh) & 1 and print 'is readonly';
- fmode($fh) & 2 and print 'is writeonly';
- fmode($fh) & 128 and print 'is readwrite';
#! perl -slw
use strict;
use Inline C => 'DATA', NAME => 'fmode', CLEAN_AFTER_BUILD => 0;
open FILE, $ARGV[0] or die "'$ARGV[0]:$!";
printf "%x\n", fmode( \*FILE );
__DATA__
C:\test>fmode "<junk.dat"
1
C:\test>fmode ">junk.dat"
2
C:\test>fmode ">>junk.dat"
2
C:\test>fmode "+<junk.dat"
80
C:\test>fmode "+>junk.dat"
80
C:\test>fmode "+>>junk.dat"
80
__C__
int fmode( FILE *stream ) {
return stream->_flag;
}
In theory you could grab the XS code generated by Inline::C and form that into a module (Win32::Fmode?) for distribution--but I've never managed to get that to work. Everytime I've tried, xsubpp bellyaches about being unable to parse stuff:
C:\Perl\bin\perl.exe C:\Perl\lib\ExtUtils/xsubpp
-typemap C:\Perl\lib\ExtUtils\typemap
Fmode.xs > Fmode.xsc && C:\Perl\bin\perl.exe
-MExtUtils::Command -e mv Fmode.xsc Fmode.c
Error: Cannot parse function definition from
'fmode( FILE *stream ) {' in Fmode.xs, line 12
And since none {{censored}} ....
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.