Mr. Shawn H. Corey wrote:
> On Sat, 2006-15-04 at 09:32 -0400, Steve Bertrand wrote:
>>Just out of curiosity, if you pass the hash, won't it create a 'copy' of
>>the original, manipulate it so that in the end you may have two
>>different versions (modifications) of the same hash? 
>>
>>As I understand it, if you pass a href, then you actually work with the
>>original hash without making a copy? Do I understand this right?
> 
> No, when Perl passes a hash, it converts it into a list. This list is
> disassociated with the hash. Any changes in the subroutine will not
> effect the original, unlike arrays:
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> use Data::Dumper;
> $Data::Dumper::Sortkeys = 1;
> 
> my @a = qw/ a b c d e f /;
> print Dumper [EMAIL PROTECTED];
> foo( @a );
> print Dumper [EMAIL PROTECTED];
> 
> my %h = (
>   a => 1,
>   b => 2,
>   c => 3,
> );
> print Dumper \%h;
> foo( %h );
> print Dumper \%h;
> 
> 
> sub foo {
>   for ( @_ ){
>     tr/aeiou/AEIOU/;
>   }
> }

Actually it will effect the values of the hash but not the keys:

$ perl -le'
sub foo {
    for ( @_ ) {
        tr/a-z/A-Z/
        }
    }
my @a = qw/ a b c d e f g h i j /;
print "@a";
foo @a;
print "@a";
my %h = qw/ a b c d e f g h i j /;
print "@{[ %h ]}";
foo %h;
print "@{[ %h ]}";
'
a b c d e f g h i j
A B C D E F G H I J
e f c d a b g h i j
e F c D a B g H i J




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to