Local scope for file handles?

Doug Sparling sparling at uclick.com
Wed May 10 16:45:30 CDT 2000


/----------------------------------------------
|Hey folks!
|
|Is there to locally scope file handles?
|
|I use:
|
|	my $this = 'that';
|
|all the time, so I tried:
|
|	my FILEHANDEL;
|
|But it just generates an error.
|
|Help! :)
|
|Erik
/-----------------------------------------------

Try:
--

#!/usr/bin/perl -w

sub read_file {
    my $path = shift;
    local *FH;  # use local, not my
    open(FH, $path) or die "can't open $path: $!\n";
    local $/ = undef;  # slurp full file
    local $_ = <FH>;
    close (FH);
    return $_;
}

$contents = read_file('/etc/passwd');
print "$contents\n";



More information about the Columbia-pm mailing list