in reply to How to get the name of an aliased function

That's a shame, since you can get the right results with XS function aliases:
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #define SI_NOT 0x01 #define SI_REV 0x02 MODULE = String::Index PACKAGE = String::Index int cindex(SV *str, SV *cc, ...) PROTOTYPE: $$;$ ALIAS: ncindex = 1 crindex = 2 ncrindex = 3 CODE: { /* you use the 'ix' variable in here to determine which alias of the function was called. ix = 0 means 'cindex', 1 means ncindex, and so on */ }
I suppose then you could get a similar effect in Perl like so:
sub sub_alias (\&;$) { my ($func, $ix) = (@_, 0); sub { $func->($ix, @_) }; # that's sub { ... }'s @_, not sub_alias' +s @_ } *foo = sub_alias &bar, 1; *blat = sub_alias &bar, 2; *gunk = sub_alias &bar, 3;
The only catch here is that when you call bar(), there won't be that extra argument at the beginning. You can be sneaky, therefore, and do something like this:
sub SubAliasIndex::new { my ($class, $ix) = @_; bless \$ix, $class; } sub SubAliasIndex::ix { ${ $_[0] } } sub sub_alias (\&;$) { my ($func, $ix) = (@_, 0); sub { $func->(SubAliasIndex->new($ix), @_) }; } sub bar { my $ix = (@_ and UNIVERSAL::isa($_[0], 'SubAliasIndex')) ? shift->ix + : 0; # ... } *foo = sub_alias &bar, 1; *blat = sub_alias &bar, 2; *gunk = sub_alias &bar, 3;
It feels like overkill to me. ;)

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart