Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Refactor method calls or not?

by Aristotle (Chancellor)
on Jan 20, 2002 at 14:41 UTC ( #140198=note: print w/replies, xml ) Need Help??


in reply to Refactor method calls or not?

Of all the replies, ++tilly's seems both closest to the original goal as well cleanest. If you really have a lot of such methods, I'd add a hash that maps add_financial_diary => 'financialDiary' so that a foreach in BEGIN() could generate these, but of course that's an obvious detail.

On the other hand I wouldn't put AUTOLOAD aside so soon despite all the advocacy against it. Since you mention that you typically only need these methods once or twice in a program's runtime, it seems an obvious idea to skip installing the methods in the symbol table altogether, in which case I find it can be written pretty cleanly. Imagine japhy's code here, but with the innermost if block replaced with something like (where _generic_add() directly does the job rather than generating a closure of course):

my $table = $calls{$meth} || croak "Unknown table action '$meth'"; $self->_generic_add($table, @_);

It is easy to substitute this for a loop in BEGIN() that installs routines which call _generic_add() if you later need more performance - and the only place that changes is a shift of code from AUTOLOAD to BEGIN, the rest of the module remains untouched, including the generic add routine and the "dispatch" hash.

Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140198]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-04-23 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?