[Chicago-talk] Class::Accessor uses a hashref - a question of style

Steven Lembark lembark at wrkhors.com
Sun Jan 25 17:57:30 CST 2004

> I've also heard it used in times where someone wants
> to keep open the option of passing in additional
> arguments outside of that hash value.
> ($class->new(\%args, $someothervar)). Again, I'd
> personally rather stick 'em in that args hash.

That depends on who is doing the initialization. If
\%args is being passed back to a parent class then
the only clean way to process the additional stuff
may be with add'l arguments. They can also clean up
the interface by standardizing the data passed in
among multiple classes with directives to the
individual dervied or speical-purpose classes comming
in as after-data control switches.

> The only other possible advantage I can conceive of is
> that you're not required to de-reference a hashref if
> you already have one.

It also depends heavily on where the arguments are
being assembled. You may find that something like:

	my $foo = complicated_call @lotsaargz;

	blah->new( $foo );

simplifies the whole thing -- and allows dumping the
contents before calling the constructor with them.

You may also have cases where the arguments are
themselves an object, say one that does lazy
initilization of itself. In this case passing it
in as an array will break the interface -- or
initialize all of the data too soon when the object
is referenced.

Net result, is that if the caller depends on a referent
you're probably better off passing them in that way to
avoid possible side-effects.

The original email's examples were trivial enough
that it wouldn't matter. In cases complicated enough
to require code auto-generator modules you are better
off being rather careful about it.

Steven Lembark                               2930 W. Palmer
Workhorse Computing                       Chicago, IL 60647
                                            +1 888 359 3508

More information about the Chicago-talk mailing list