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

Discussion in 'Perl Misc' started by Sven Wolf, Dec 3, 2004.

  1. Sven Wolf

    Sven Wolf Guest

    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.
     
    Sven Wolf, Dec 3, 2004
    #1
    1. Advertising

  2. 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};


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 3, 2004
    #2
    1. Advertising

  3. Sven Wolf

    Anno Siegel Guest

    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
     
    Anno Siegel, Dec 3, 2004
    #3
  4. Sven Wolf

    Sven Wolf Guest

    Tad McClellan <> wrote in message news:<>...
    > 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};



    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?
     
    Sven Wolf, Dec 3, 2004
    #4
  5. Sven Wolf

    Sven Wolf Guest

    -berlin.de (Anno Siegel) wrote in message news:<copo7j$6o5$-Berlin.DE>...
    > 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.
     
    Sven Wolf, Dec 3, 2004
    #5
  6. Sven Wolf

    Ben Morrow Guest

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

    >
    > 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]
     
    Ben Morrow, Dec 3, 2004
    #6
  7. Sven Wolf

    Ben Morrow Guest

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

    Quoth (Sven Wolf):
    > -berlin.de (Anno Siegel) wrote in message
    > news:<copo7j$6o5$-Berlin.DE>...
    > > Tad McClellan <> wrote in comp.lang.perl.misc:
    > > > Sven Wolf <> 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::eek: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]
     
    Ben Morrow, Dec 3, 2004
    #7
  8. Sven Wolf <> wrote:
    > -berlin.de (Anno Siegel) wrote in message news:<copo7j$6o5$-Berlin.DE>...
    >> Tad McClellan <> wrote in comp.lang.perl.misc:
    >> > Sven Wolf <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 3, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    0
    Views:
    279
  2. Jim Hunter
    Replies:
    3
    Views:
    628
    Jim Hunter
    Mar 6, 2005
  3. Hadmut Danisch

    Testing global variables for existence?

    Hadmut Danisch, Dec 16, 2004, in forum: Ruby
    Replies:
    2
    Views:
    112
    Hadmut Danisch
    Dec 16, 2004
  4. still just me

    Testing File Existence - best way?

    still just me, Dec 13, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    116
    Juha Laiho
    Dec 27, 2007
  5. Eric

    testing for existence of file

    Eric, May 16, 2006, in forum: Javascript
    Replies:
    4
    Views:
    117
    Thomas 'PointedEars' Lahn
    May 22, 2006
Loading...

Share This Page