Thank you very much Schlomi - I stand corrected!
Frank, another way of putting what Schlomi has illustrated is that:
%{$self->{'DATA'}} = ( foo => 'bar' );
means "change the contents of the hash which $self->{'DATA'} refers to",
while
$self->{'DATA'} = { foo => 'bar' };
means "change $self->{'DATA'} to refer to a different hash"
Andrew
On Sun, Apr 12, 2015 at 9:05 AM, Shlomi Fish <[email protected]> wrote:
> Hi Andrew,
>
> On Sun, 12 Apr 2015 07:25:55 +0100
> Andrew Solomon <[email protected]> wrote:
>
> > Hi Frank
> >
> > I found the first one rather obscure, but they are equivalent.
>
> No - they are not exactly the same. See below:
>
> > To prove
> > this, Data::Dumper is my friend:
> >
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> > use Data::Dumper;
> >
> > {
> > my $self;
> >
> > print "selfish self \n";
> > %{$self->{'DATA'}} = ( foo => 'bar' );
> > print Dumper $self;
> > }
> >
> > {
> > my $self;
> >
> > print "selfless self \n";
> > $self->{'DATA'} = { foo => 'bar' };
> > print Dumper $self;
> > }
> >
>
> However, with this program:
>
> < CODE >
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use Data::Dumper;
>
> {
> my $self;
>
> $self->{'DATA'} = { one => 'two', };
> my $slot = $self->{'DATA'};
>
> print "selfish self \n";
> %{$self->{'DATA'}} = ( foo => 'bar' );
> print Dumper $slot;
> }
>
> {
> my $self;
>
> $self->{'DATA'} = { one => 'two', };
> my $slot = $self->{'DATA'};
>
> print "selfless self \n";
> $self->{'DATA'} = { foo => 'bar' };
> print Dumper $slot;
> }
>
> < / CODE >
>
> I am getting this output:
>
> < SHELL >
>
> shlomif@telaviv1:~$ perl Test-hash-clear.pl
> selfish self
> $VAR1 = {
> 'foo' => 'bar'
> };
> selfless self
> $VAR1 = {
> 'one' => 'two'
> };
> shlomif@telaviv1:~$
>
> < / SHELL >
>
> So as can be seen, they are not exactly equivalent.
>
> Regards,
>
> Shlomi Fish
>
> --
> -----------------------------------------------------------------
> Shlomi Fish http://www.shlomifish.org/
> Escape from GNU Autohell -
> http://www.shlomifish.org/open-source/anti/autohell/
>
> <rindolf> If you repeat a scene 50k times, then the movie will have less
> entropy and will compress better. ( irc://irc.freenode.org/#perlcafe )
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>
--
Andrew Solomon
Mentor@Geekuni http://geekuni.com/
http://www.linkedin.com/in/asolomon