develooper Front page | perl.module-authors | Postings from November 2003

Re: Class::FakeAttributes -- Opinions Wanted

Thread Previous | Thread Next
From:
Smylers
Date:
November 4, 2003 01:31
Subject:
Re: Class::FakeAttributes -- Opinions Wanted
Message ID:
slrnbqdmmj.pi.Smylers@stripey.com
Orton, Yves writes:

> > (There's equivalent reasoning behind the existence of 
> > push_attribute().)
> 
> But in this case its unwarranted.
> 
>   my $foo=undef;
>   push @$foo,"Bar";
>   print "@$foo\n";

But if the undef is returned from a function, the autovivification only
happens to the local copy, not the version used by the function:

  my %hash;
  sub a { return $hash{foo} }
  my $foo = a();
  push @$foo, "Bar";
  print "@$foo\n";
  print "@{$hash{foo}}\n";

Compare with:

  my %hash;
  push @{$hash{foo}}, 'Hello';
  sub a { return $hash{foo} }
  my $foo = a();
  push @$foo, "Bar";
  print "@$foo\n";
  print "@{$hash{foo}}\n";

Hence the need for a method that always results in pushing on to the
array reference stored in the hash, regardless of whether it already has
any values in there.

Smylers


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About