[Wellington-pm] Creating an array of objects

Ewen McNeill ewen at naos.co.nz
Sat Mar 11 10:33:51 PST 2006

In message <44129124.9080300 at cliffp.com>, Cliff Pratt writes:
>What I want to do is create an array of objects. Or is that an array of
>object references. I'm not sure.   [...]
>my $foo = MyPackage->new('bar') ;

For the special case (one init parameter, or perhaps one varying init
parameter) you could do something like this:

my @init = qw(tut ankh amun);
my @foo  = map { MyPackage->new($_) } @init;

If you need more parameters you could perhaps index a hash or set of
arrays with those values.

Generally if you need lots of objects then the source information for
them comes from somewhere else (eg, a database, a document, etc) and
you can create them (or have them created for you) as you process that
thing.  So the structure of what you're processing naturally suggests
the way to create the objects.

And FWIW in perl what you get is an object reference pretty much all the
time.  (In theory you could get a copy of the underlying
scalar/array/hash that is blessed to be the object, but in practice
that's not very useful in perl.)  Other OO languages handle this
differently in some situations.


More information about the Wellington-pm mailing list