>>>>> "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