Charles K. Clarkson <> wrote:
> Petr Vileta wrote:
> 
>> I have public hash and in sub{}
> 
>     A public hash sounds like a really bad idea. Why not use a
> lexically scoped hash and specifically pass it into the sub? 
> 
> 
>> Example with success but maybe is possible to write it better:
>> 
>> our $hash;
>> $hash->{first} = 1;
>> $hash->{second} = 2;
>> &mysub;
>> print $hash->{second};
>> 
>> sub mysub {
>>     my $new_hash;
>>     # make local copy
>>     $new_hash->{$_} = $hash->{$_} foreach (keys %{$hash});
>>     print $new_hash->{second};
>>     $new_hash->{second} = 10;
>>     print $new_hash->{second};
>> }
> 
> 
> my $hash = {
>     first   => 1,
>     second  => 2,
> };
> 
> mysub( $hash );
> 
> print $hash->{second};
> 
> sub mysub {
>     my %copy = %{ shift @_ };
> 
>     print $copy{second};
>     $copy{second} = 10;
>     print $copy{second};
> }
> 
> __END__
> 

This, and other suggestions, performs a shallow copy, which is fine for
the example given where the hash values are scalars. If, on the other
hand, the values were hashes containing arrays of objects that contained
arrays of hashes, for example, a shallow copy may not be good enough. A
deep copy is required that follows every reference and copies whatever
it refers to, which is a lot more complex. Luckily, however, the
Activestate distribution provides modules that can do this. See
Data::Dumper and Storable.

> 
>> (My server reject all messages from Yahoo and Hotmail. Send me
>> your mail from another non-spammer site please.)
> 
>     Replace "reject" with "rejects"

Or perhaps "will reject".

HTH

-- 
Brian Raven 


=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the 
intended addressee(s). Unauthorised reproduction, disclosure, modification, 
and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message do not 
necessarily reflect those of Atos Euronext Market Solutions.

L'information contenue dans cet e-mail est confidentielle et uniquement 
destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. 
Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail 
vous parvient par erreur, nous vous prions de bien vouloir prevenir 
l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre 
systeme. Le contenu de ce message electronique ne represente pas necessairement 
la position ou le point de vue d'Atos Euronext Market Solutions.


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to