Using { } instead of \ does the same thing
Not exactly. Consider:
#! perl
use strict;
use warnings;
use Data::Dumper;
my %hash = ( Fred => 'Wilma', Barney => 'Betty' );
my $ref = \%hash;
my $copy = { %hash };
$hash{Homer} = 'Marge'; # Alter the original hash
print "\nReference: ", Dumper($ref);
print "\nCopy: ", Dumper($copy);
Output:
0:10 >perl 606_SoPW.pl
Reference: $VAR1 = {
'Homer' => 'Marge',
'Barney' => 'Betty',
'Fred' => 'Wilma'
};
Copy: $VAR1 = {
'Barney' => 'Betty',
'Fred' => 'Wilma'
};
0:10 >
A backslash \ produces a reference to the same hash, but braces { } create a reference to a new, separate, anonymous hash containing a copy of the data in the original.
Hope that helps,
|