Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Lexical file handles

Reply
Thread Tools

Lexical file handles

 
 
Tintin
Guest
Posts: n/a
 
      11-06-2004
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.


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-06-2004
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
 
Reply With Quote
 
 
 
 
Ala Qumsieh
Guest
Posts: n/a
 
      11-06-2004
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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-06-2004
>>>>> "GH" == Gunnar Hjalmarsson <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      11-06-2004


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

 
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
How to do Lexical analysis for c/c++ source file? abhisheknayak9 C++ 0 06-08-2012 03:37 AM
File::Copy::copy With File Handles MaggotChild Perl Misc 2 10-22-2011 12:15 AM
Question about file handles and windows handles @ Windows Operating Systems eino Python 1 05-08-2007 09:14 PM
Tie and Lexical Filehandles or IO::File? Veli-Pekka Tštilš Perl Misc 2 08-28-2006 08:11 PM
Integrating FILE * and int file handles Denis Remezov C++ 12 05-11-2004 08:20 PM



Advertisments