Filehandles

Discussion in 'Perl Misc' started by Guenther Sohler, Sep 29, 2005.

  1. Hallo,

    I am writing a small parser in perl, which should also be able to
    include files.
    if a include statement happens,
    the prg shall put the current FH onto the stack and open a new file with
    the same FH.

    How can I do this as filehandles are special in perl ?
     
    Guenther Sohler, Sep 29, 2005
    #1
    1. Advertising

  2. Also sprach Guenther Sohler:

    > I am writing a small parser in perl, which should also be able to
    > include files.
    > if a include statement happens,
    > the prg shall put the current FH onto the stack and open a new file with
    > the same FH.
    >
    > How can I do this as filehandles are special in perl ?


    Just pretend that they aren't special. Filehandles no longer need to be
    barewords:

    open my $fh, "<", $file or die "$file: $!";
    push @stack, $fh;

    ...

    # and then later
    my $element = pop @stack;
    if (ref($element) eq 'GLOB') { # is it a filehandle?
    while (<$element>) { # then: read file
    ...
    }
    }

    Note that your terminology is a bit sloppy: If it's a filehandle then
    the underlying file has already been opened and you cannot really "open
    a new file with the same FH".

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Sep 29, 2005
    #2
    1. Advertising

  3. Guenther Sohler

    Anno Siegel Guest

    Guenther Sohler <> wrote in comp.lang.perl.misc:
    > Hallo,
    >
    > I am writing a small parser in perl, which should also be able to
    > include files.
    > if a include statement happens,
    > the prg shall put the current FH onto the stack and open a new file with
    > the same FH.
    >
    > How can I do this as filehandles are special in perl ?


    You use what is known as a lexical filehandle:

    open my $fh, $file;

    These are (slightly simplified) lexical variables that contain a reference
    to an anonymous filehandle. The filehandle itself is still global.

    Also search CPAN for the keyword "include". What comes up looks like
    your problem has been dealt with before.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Sep 29, 2005
    #3
  4. Guenther Sohler <> wrote:

    > put the current FH onto the stack


    > How can I do this as filehandles are special in perl ?



    perldoc -q filehandle


    How can I make a filehandle local to a subroutine? How do I
    pass filehandles between subroutines? How do I make an array
    of filehandles?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 29, 2005
    #4
  5. Tassilo v. Parseval wrote:

    > Note that your terminology is a bit sloppy: If it's a filehandle then
    > the underlying file has already been opened and you cannot really "open
    > a new file with the same FH".


    Er, no. Perl filehandles (aka "IO thingys") can exist in a closed state
    and a new file can be associated with an existinh file handle with open().
     
    Brian McCauley, Sep 29, 2005
    #5
  6. Brian McCauley wrote:

    > Tassilo v. Parseval wrote:
    >
    >> Note that your terminology is a bit sloppy: If it's a filehandle then
    >> the underlying file has already been opened and you cannot really "open
    >> a new file with the same FH".

    >
    >
    > Er, no.


    Ignore that.

    I now realise you were drawing a destinction between "file handle" and
    "IO handle". When a filehandle is closed it just becomes a closed IO
    handle.
     
    Brian McCauley, Sep 29, 2005
    #6
    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. Jeremy Phillips

    newbie - Output to multiple Filehandles

    Jeremy Phillips, May 24, 2004, in forum: Perl
    Replies:
    1
    Views:
    2,142
    Jürgen Exner
    May 25, 2004
  2. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    1
    Views:
    639
    Jürgen Exner
    Jul 10, 2004
  3. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    0
    Views:
    613
    Andrew
    Jul 10, 2004
  4. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    0
    Views:
    656
    Andrew
    Jul 10, 2004
  5. Sako
    Replies:
    3
    Views:
    5,618
Loading...

Share This Page