Hello perl gurus,

I spent almost a whole day solving this problem.  However, I wonder why it's a problem.

# ////////////////////////////////////////////////////
use strict;
no strict "refs" ;

# this works fine, produces three files, aa , bb, cc with contents 'xxx aa xxx' etc

my    %fh = ( aa => 1, bb =>2, cc => 3) ;  # note that each instance of $fh{$fhkey} has a different value

foreach my $fhkey ( keys %fh){
    open  ( $fh{$fhkey}, ">$fhkey" )  ;   
}
               
foreach my $fhkey ( keys %fh ){
    print { $fh{$fhkey} }  "xxx $fhkey xxx \n" ;    # note file handle $fh{$fhkey} has to be in a block
} ;

foreach my $fhkey ( keys %fh ){
    close $fh{$fhkey} ;   
}

#------------------------------------------------------------
# The following program (which is what I started out with) will NOT work properly:  Each element of %fh has a value of 1
# Why doesn't this work properly?  - all statements write to one file
#------------------------------------------------------------


my    %fh = ( aa => 1, bb =>1, cc => 1) ;    # note - only change, each $fh{} has value of 1

foreach my $fhkey ( keys %fh){
    open  ( $fh{$fhkey}, ">$fhkey" )  ;   
}
               
foreach my $fhkey ( keys %fh ){
    print { $fh{$fhkey} }  "xxx $fhkey xxx \n" ;    # note file handle $fh{$fhkey} has to be in a block   
} ;

foreach my $fhkey ( keys %fh ){
    close $fh{$fhkey} ;   
}

# ////////////////////////////////////////////////////
Hugh Loebner
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to