Lexical file handles

Discussion in 'Perl Misc' started by Tintin, Nov 6, 2004.

  1. Tintin

    Tintin Guest

    Now that recent versions of Perl have lexical file handles, can anyone give
    me some practical examples of why they are useful/better.

    The only thing I can think of is that with 'use strict', you'll catch typos
    in your filehandles, however I'm sure there's plenty of other reasons.
    Tintin, Nov 6, 2004
    #1
    1. Advertising

  2. Tintin wrote:
    > Now that recent versions of Perl have lexical file handles, can
    > anyone give me some practical examples of why they are useful/better.
    >
    > The only thing I can think of is that with 'use strict', you'll catch
    > typos in your filehandles, however I'm sure there's plenty of other
    > reasons.


    They are automatically closed when out of scope.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 6, 2004
    #2
    1. Advertising

  3. Tintin

    Ala Qumsieh Guest

    Tintin wrote:

    > Now that recent versions of Perl have lexical file handles, can anyone give
    > me some practical examples of why they are useful/better.


    They are not global anymore.

    Like any other lexical variable, filehandles should be constrained to
    the narrowest scope possible. One side effect of undef()ing a lexical
    filehandle is that it closes the handle. So, I often do this:

    {
    open my $fh, $file or die ...;
    while (<$fh>) { ... }
    }

    This will automatically close my filehandle upon reaching the end of the
    block. It also lets me use $fh for every file I open (assuming I have
    only one open at a time) with no fear of stomping on other variables.

    It is also easier to pass it to subroutines as argument.

    --Ala
    Ala Qumsieh, Nov 6, 2004
    #3
  4. Tintin

    Uri Guttman Guest

    >>>>> "GH" == Gunnar Hjalmarsson <> writes:

    GH> Tintin wrote:
    >> Now that recent versions of Perl have lexical file handles, can
    >> anyone give me some practical examples of why they are useful/better.
    >> The only thing I can think of is that with 'use strict', you'll catch
    >> typos in your filehandles, however I'm sure there's plenty of other
    >> reasons.


    GH> They are automatically closed when out of scope.

    so were localized type globs.

    lexical handles are better since they are lexical. file globs (or plain
    text file handles are always package scoped (global to the package). so
    using them could clobber another file with the same name in the same
    package. you can't do that with lexical handles. the old way to get a
    clean handle was calling Symbol::gensym which returned an anonymous
    typeglob so no other code could access the handle. another trick was
    something like this: my $fh = do{ local *FOO ; \*FOO }.

    so the deep functionality of lexical handles was always available. but
    now it is simpler to use in open (and IPC::Open[23] uses them too now
    IIRC).

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 6, 2004
    #4
  5. Ala Qumsieh wrote:

    > {
    > open my $fh, $file or die ...;
    > while (<$fh>) { ... }
    > }
    >
    > This will automatically close my filehandle upon reaching the end of the
    > block. It also lets me use $fh for every file I open (assuming I have
    > only one open at a time)


    Actually the one open at a time restriction does not apply. You can
    have dozens of variables called $fh in different scopes. That's like
    the whole point.

    It would be dubious coding style to have one $fh inside an inner lexical
    scope masking another but there's no reason why you can't have closures
    that have file handles.

    sub make_reader {
    my $file = shift;
    open my $fh, '<', $file or die "open $file: $!";
    sub {
    my $line = <$fh>;
    # Transform $line
    $line;
    }
    }
    Brian McCauley, Nov 6, 2004
    #5
    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. Denis Remezov
    Replies:
    12
    Views:
    3,835
    Denis Remezov
    May 11, 2004
  2. eino
    Replies:
    1
    Views:
    423
    =?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=
    May 8, 2007
  3. Veli-Pekka Tätilä

    Tie and Lexical Filehandles or IO::File?

    Veli-Pekka Tätilä, Aug 28, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    173
    Veli-Pekka Tätilä
    Aug 28, 2006
  4. MaggotChild

    File::Copy::copy With File Handles

    MaggotChild, Oct 18, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    491
    Ilya Zakharevich
    Oct 22, 2011
  5. abhisheknayak9

    How to do Lexical analysis for c/c++ source file?

    abhisheknayak9, Jun 8, 2012, in forum: C Programming
    Replies:
    0
    Views:
    1,302
    abhisheknayak9
    Jun 8, 2012
Loading...

Share This Page