Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: check scalar holds filehandle

by Discipulus (Canon)
on Oct 30, 2015 at 09:08 UTC ( [id://1146462]=note: print w/replies, xml ) Need Help??


in reply to Re: check scalar holds filehandle
in thread check scalar holds filehandle

but that doesn't work for ram file handles.
is not  fileno ($mem) == -1 the right way to tell if an handle is opened onto a memory file? and reliably be sure that is an handle anyway? The only minus is that for memory filehandle we cant be sure they point to the same point because they all returns -1.
# real file perl -Mstrict -wE "open my $fh,'<','out.log' or die;say fileno($fh) ? +'FileHandle! '.fileno($fh):'NA'" FileHandle! 3 # memory file perl -Mstrict -wE "open my $fh,'<',\my $mem or die;say fileno($fh) ? ' +FileHandle! '.fileno($fh):'NA'" FileHandle! -1 # not existing file, closed filehandles, aliens things.. perl -Mstrict -wE "open my $fh,'<','out.logXX';say fileno($fh) ? 'File +Handle! '.fileno($fh):'NA'" NA
so you dont need tell to tell!

L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^3: check scalar holds filehandle
by BrowserUk (Patriarch) on Oct 30, 2015 at 09:16 UTC

    In 5.10 perlfunc#fileno, it used to say:

    (Filehandles connected to memory objects via new features of open may return undefined even though they are open.)

    I guess I haven't had cause to notice the change since I discovered that piece of wisdom.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: check scalar holds filehandle
by afoken (Chancellor) on Oct 31, 2015 at 07:57 UTC
    /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". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1146462]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-19 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found