/home/alex>perl -Mstrict -w -E 'say fileno(STDERR) ? "Filehandle" : "N +A"' Filehandle /home/alex>perl -Mstrict -w -E 'say fileno(STDOUT) ? "Filehandle" : "N +A"' Filehandle /home/alex>perl -Mstrict -w -E 'say fileno(STDIN) ? "Filehandle" : "NA +"' NA /home/alex>

Oooops ...

In a program running in background, you might have closed STDIN, this will very likely make your next opened file have a fileno of 0.

/home/alex>perl -Mstrict -w -E 'open my $f,"<","/dev/null"; say fileno +($f)' 3 /home/alex>perl -Mstrict -w -E 'close STDIN; open my $f,"<","/dev/null +"; say fileno($f)' 0 /home/alex>

So better use defined:

/home/alex>perl -Mstrict -w -E 'my $f="foo"; say defined(fileno($f)) ? + "filehandle" : "not a filehandle"' not a filehandle /home/alex>perl -Mstrict -w -E 'open my $f,"<","/dev/null"; say define +d(fileno($f)) ? "filehandle" : "not a filehandle"' filehandle /home/alex>perl -Mstrict -w -E 'open my $f,"<",\my $mem; say defined(f +ileno($f)) ? "filehandle" : "not a filehandle"' filehandle /home/alex>perl -Mstrict -w -E 'close STDIN; open my $f,"<","/dev/null +"; say defined(fileno($f)) ? "filehandle" : "not a filehandle"' filehandle /home/alex>

Update:

fileno called on a string has some nasty surprises:

/home/alex>perl -Mstrict -w -E 'say fileno(STDERR); say fileno("STDERR +"); say fileno("stderr"); say fileno("foobar")//"undef"; say fileno($ +_)//"undef" for (qw( STDERR stderr foo ))' Name "main::foobar" used only once: possible typo at -e line 1. 2 2 2 undef 2 2 undef /home/alex>

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^3: check scalar holds filehandle by afoken
in thread check scalar holds filehandle by bagyi

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.