Hugh Loebner wrote:

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

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

The proper way to do this would be to use Symbol:

use Symbol;
my %fh = (aa => gensym, bb => gensym, cc => gensym);

Barring that, you could use a ref to typeglob :

my %fh = (aa => \*1, bb => \*2, cc => \*3);

If you use 1,1,1 instead of 1,2,3 - then they'll all be pointing to the
same symbolic reference and essentially the same file.

> 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} ;   
> }

-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to