note
mascip
<p>More solutions, cool :-)</p>
<p>
I see that lanX's answer is the "good" clean answer, but i don't like it as a stand-alone answer, as the code then looks ugly (i forgot to say it's a method, not a function) :
<code>
$S{light}->($self, 10);
</code> instead of
<code>
$self->light(10)
</code>
(I used <code>%S</code> instead of <code>%mysubs</code>. And i use Moose, so i need to pass $self as an argument).
</p>
<p>
So, what i did was to use AUTOLOAD, to call these subroutines :
<code>
sub AUTOLOAD {
my $self = shift;
my @args = @_;
# Retrieve the method name, without the Package name
my $name = our $AUTOLOAD;
$name =~ s/.*://;
# Check if this is one of the functions we declared
croak "Undefined method : $name"
if !$S{$name};
# Call the appropriate method
return $S{$name}->($self, @args);
}
</code>
which enables me to call
<code>
$obj->light(10);
</code>
</p>
<p>
Now, the question is : is that clean, and are there "cleaner" (and why) solutions.
</p>
<p>
I must say i'm not in love with the 'eval' solution ( but it works :-) ).
</p>
<p>
Would it be cleaner to do
<code>
foreach my $name (@names) {
my $name = $S{$name};
}
</code>
But then i will also need to put <code>$self</code> as an argument all the time.
</p>
<p>
What about installing them in the import table : is it cleaner than using AUTOLOAD ?
</p>
990016
990044