phl.jns has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am using the following in a script to use a variable as a filehandle:
opendir (my $sub_dir, $dir) or die "Couldn't open $dir : $!\n"; while (defined (my $file = readdir($sub_dir)) )
I am trying to run the script on an older box that has perl version 5.005_03 on it - the method I have used will not work pre v5.8. What other methods can I use to scope filehandles ? The subroutine that uses this line in my script will be called recursively and I don't want to trample my open filehandles. Thanks, Phil

Replies are listed 'Best First'.
Re: Lexical scoping for filehandles, pre perl 5.8
by jwkrahn (Abbot) on Oct 23, 2008 at 11:00 UTC
Re: Lexical scoping for filehandles, pre perl 5.8
by Anonymous Monk on Oct 23, 2008 at 11:01 UTC
    I think lexical handles were available since perl 5.6, anyway, use local, as in
    { local HANDLE; opendir HANDLE, $dir or die "Couldn't open $dir : $!\n"; ... } # left scope, HANDLE is what it was before
      That just produces the following error:- Can't modify constant item in local at ... That happpens on v5 and v5.8
        fixed now! Had put an asterisk in front of the filehandle name:-
        local *HANDLE
        Thanks, Phil