Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > testing for existence of an inline file with Inline::Files

Reply
Thread Tools

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

 
 
Sven Wolf
Guest
Posts: n/a
 
      12-03-2004
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.
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      12-03-2004
Sven Wolf <(E-Mail Removed)> 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};


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      12-03-2004
Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Sven Wolf <(E-Mail Removed)> 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
 
Reply With Quote
 
Sven Wolf
Guest
Posts: n/a
 
      12-03-2004
Tad McClellan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> Sven Wolf <(E-Mail Removed)> 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};



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?
 
Reply With Quote
 
Sven Wolf
Guest
Posts: n/a
 
      12-03-2004
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote in message news:<copo7j$6o5$(E-Mail Removed)-Berlin.DE>...
> Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > Sven Wolf <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-03-2004

Quoth (E-Mail Removed) (Sven Wolf):
> Tad McClellan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> > Sven Wolf <(E-Mail Removed)> 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};

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

--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] (E-Mail Removed)
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-03-2004

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

Quoth (E-Mail Removed) (Sven Wolf):
> (E-Mail Removed)-berlin.de (Anno Siegel) wrote in message
> news:<copo7j$6o5$(E-Mail Removed)-Berlin.DE>...
> > Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > > Sven Wolf <(E-Mail Removed)> wrote:
> > > >
> > > > 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?

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

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


Try Scalar::Util:penhandle.

Ben

--
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased] (E-Mail Removed)
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-03-2004
Sven Wolf <(E-Mail Removed)> wrote:
> (E-Mail Removed)-berlin.de (Anno Siegel) wrote in message news:<copo7j$6o5$(E-Mail Removed)-Berlin.DE>...
>> Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> > Sven Wolf <(E-Mail Removed)> wrote:


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



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



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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing File Existence - best way? still just me Perl Misc 2 12-27-2007 03:57 PM
testing for existence of file Eric Javascript 4 05-22-2006 09:44 PM
testing for existence of compilers/executables John Python 0 03-05-2006 04:36 PM
Checking fprintf for errors and testing for existence of a file Jim Hunter C Programming 3 03-06-2005 05:29 PM
Testing global variables for existence? Hadmut Danisch Ruby 2 12-16-2004 05:51 PM



Advertisments