(Anno Siegel) wrote in message news:<copo7j$6o5$>...
> Tad McClellan <> wrote in comp.lang.perl.misc:
> > Sven Wolf <> wrote:
> > > Hello,
> > >
> > > I'm using Inline::Files, which allows me to put data after __FOO__ in
> > > a file and then to read it with "while (<FOO>) {...}" or @lines =
> > ><FOO>;
> >
> >
> > > I get a read-from-unopened-filehandle error if there is no section
> > > __MYCANDIDATE__ in the script.
> > >
> > > Can someone suggest a test to apply before attempting to read?
> >
> >
> > The docs say that a package variable named $MYCANDIDATE will
> > exist if there is such a section. You can just look in the
> > symbol table to see if one by that name is there or not.
> >
> > This seems to work for me:
> >
> > die 'no such section' unless exists $main::{MYCANDIDATE};
>
> More generally, fileno( HANDLE) will be defined if and only of HANDLE
> is an open filehandle. In particular,
>
> defined fileno( DATA)
>
> will tell if there is an available __DATA__ or __END__ section. I
> suppose that's how $MYCANDIDATE is set internally.
>
> Anno
Thanks. I'm getting an exception when trying this with a virtual
file.
use warnings;
use Inline::Files;
my $sec = 'FOO';
eval {
print "$sec there\n" if defined fileno("main::$sec");
};
if ($@) {
print "tried and caught $@\n";
}
__FOO__
yields
tried and caught Inline::Files::Virtual::FILENO not yet implemented at
test.pl line 6
Segmentation fault (core dumped)
I think the answer for me is just to turn off the warnings temporarily
and let Inline::Files go ahead and try to open the filehandle even if
it might not exist.
I suppose I might be behind the curve in Perl version (5.6.1) or
Inline::Files version (0.62) but those are constraints of my
environment.