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