testing for existence of an inline file with Inline::Files

S

Sven Wolf

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>;

This works fine as long as there is such a section defined. However,
I haven't been able to figure out how to check whether such a section
exists in the first place. Say I have the name of a possible section
in $sec, e.g.
$sec = 'main::MYCANDIDATE';
If I just go ahead with
@lines = <$sec>;
I get a read-from-unopened-filehandle error if there is no section
__MYCANDIDATE__ in the script. This works fine if the section
appears.

Can someone suggest a test to apply before attempting to read?

Things I've tried:
1.
open IN, "<$sec" or ...;
which doesn't work because $sec itself is the (indirect) filehandle.
2.
open $sec or ...;
open sec or ...;
both also didn't work. Both fail even if the section appears.
3.
no strict refs;
my $cand = 'MYCANDIDATE';
if (exists ${$sec}{file}) { ... }
# or: if (exists $main::{$cand}{file}) { ... }
gives the mysterious "unknown error" at runtime, though the Perl
Cookbook says that there should be a hash %MYCANDIDATE with data about
the handle to the inline file, in particular a key "file".

I have a workaround which requires users in effect to declare the
sections first, but I'd like to understand this better.
 
T

Tad McClellan

Sven Wolf said:
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};
 
A

Anno Siegel

Tad McClellan said:
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
 
S

Sven Wolf

Tad McClellan said:
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};


Yes, that works for me, too. The trick is making it work indirectly.
I have $inlinefile = 'MYCANDIDATE'. Is there an expression in terms
of the lhs?
 
S

Sven Wolf

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.
 
B

Ben Morrow

Quoth (e-mail address removed) (Sven Wolf):
Yes, that works for me, too. The trick is making it work indirectly.
I have $inlinefile = 'MYCANDIDATE'. Is there an expression in terms
of the lhs?

Errr.... did you try

die "no such section' unless exists $main::{$inlinefile};

?

%main:: is just a hash with a silly name. Note that this comes under
symrefs, so be careful.

Ben
 
B

Ben Morrow

[please wrap your articles, including the attribution, at 76 characters]

Quoth (e-mail address removed) (Sven Wolf):
(e-mail address removed)-berlin.de (Anno Siegel) wrote in message


Thanks. I'm getting an exception when trying this with a virtual
file.

Try Scalar::Util::eek:penhandle.

Ben
 
T

Tad McClellan

Sven Wolf said:
I'm getting an exception when trying this with a virtual
file.

tried and caught Inline::Files::Virtual::FILENO not yet implemented at
test.pl line 6
I think the answer for me is just to turn off the warnings temporarily


I think that is the LAST answer, to be used only when all other
possibilities have been eliminated.

Looks like Anno's idea is ahead of the module's implementation. :-(

But what is wrong with my idea then?

(apart from the small chance of having chosen a scalar variable
name identical to a data section name, in which case it wasn't I::F
that made the symbol table entry.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top