lists user wrote:
> I saw this code piece in perldoc perldata,
> 
>            sub newopen {
>                my $path = shift;
>                local  *FH;  # not my!
>                open   (FH, $path)          or  return undef;
>                return *FH;
>            }
>            $fh = newopen('/etc/passwd');

That is an *example* from the "Typeglobs and Filehandles" section.


> why it say a 'local *FH' here?

*FH is a typeglob.  It represents all the FH variables in the current package
so if the current package is 'main' the typeglob *FH is short for *main::FH
and it represents the scalar $main::FH, the array @main::FH, the hash
%main::FH, the filehandle main::FH, the directory handle main::FH, the
subroutine &main::FH and the format name main::FH.


> why not just,
> 
>     open FH,$path or return undef;
>     return *FH;

Because FH is a package variable so if you have another filehandle named FH in
the same package it will have the file it is associated with closed before the
current file is opened.



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to