/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". ;-)
|