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:
slrnbqdmuv.pi.Smylers@stripey.com
darren chamberlain writes:

> * A. Pagaltzis <pagaltzis at gmx.de> [2003-11-03 17:38]:
> 
> > * Orton, Yves <yves.orton@de.mci.com> [2003-11-03 17:27]:
> > >
> > > > If the 'foo' attribute hasn't been set to anything, then you
> > > > want an empty list to iterate over.  With version C, that's
> > > > what you get.
> > > 
> > > No. If $self->{foo} is undef you get an error with version C.
> > 
> > You're forgetting autovivification. If $self->{foo} is undef it is
> > coerced into a appropriate reftype.
> 
>   $ perl -Mstrict -wle 'my $f = { }; my @a = @{ $f->{foo} }'
>   Can't use an undefined value as an ARRAY reference at -e line 1.

Oh, so it does there -- I didn't know that, probably because taking a
copy of an array is fairly unusual.  But it does work with foreach and
push:

  $ perl -Mstrict -wle 'my $f = { }; foreach (@{$f->{foo}}) { print };
  push @{$f->{foo}}, "hello"; foreach (@{$f->{foo}}) { print }; my @a =
  @{$f->{foo}};'

That's the kind of thing I do all the time with $self when $self is a
hash-ref, and hence the desire for attribute_list() to do something
difference when I can't use a hash-ref and am using fake, I mean ...
out-of-band, attributes instead!

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