Lexical file handles

T

Tintin

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

Gunnar Hjalmarsson

Tintin said:
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.
 
A

Ala Qumsieh

Tintin said:
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
 
U

Uri Guttman

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
 
B

Brian McCauley

Ala said:
{
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;
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top