in reply to check scalar holds filehandle

In most cases you can test if a scalar contains a valid file handle by calling fileno on it; but that doesn't work for ram file handles.

I've used tell as a 'valid open file handle' test in the past and didn't find any limitations.


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.

Replies are listed 'Best First'.
Re^2: check scalar holds filehandle
by Discipulus (Canon) on Oct 30, 2015 at 09:08 UTC
    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.

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