in reply to Re: is autoload bad style?
in thread is autoload bad style?
What stopped you from generating the methods when loading the module?
I can see why you'd perhaps want to delay creation of the methods if there are several hundreds of attributes and you might not use to many of them for each compilation. But you said you chose AUTOLOAD due to flexibility. What flexibility does AUTOLOAD give you that's not easily done without AUTOLOAD? It makes think you have indeterministic attribute names, or attribute names that are best expressed through a pattern. But you also say that you have a list with the attributes that are allowed for autogeneration. So it doesn't sound like it's that either.
In either case I'd prefer to have generic &set_value and &get_value methods instead, where the first argument is the attribute name. This would also allow you to easier give better diagnostics, and if there is a couple of hundred attributes I'd expect a typo or two every now and then. Furthermore, if I needed to initialize more than a handful attributes I'd prefer, if possible, doing something like
my %attrs = ( foo => 1, bar => 2, ..., baz => 50, ); while (my ($attr, $val) = each %attrs) { $obj->set_value($attr => $val); }
over
$obj->set_value_foo(1); $obj->set_value_bar(2); ...; $obj->set_value_baz(50);
or being forced to do some dynamic method lookup, e.g.
my %attrs = ( foo => 1, bar => 2, ..., baz => 50, ); while (my ($attr, $val) = each %attrs) { $obj->${\"set_value_$attr"}($val); }
I really don't see the advantage of having an AUTOLOAD here. I just see disadvantages. Look at Re: is autoload bad style? for an explanation for my skepticism.
If you do want to pre-generate the methods there are modules for that on CPAN to make it even simpler for you.
ihb
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: is autoload bad style?
by strat (Canon) on Jul 27, 2003 at 10:51 UTC | |
by Anonymous Monk on Jul 31, 2003 at 00:48 UTC |