I suppose then you could get a similar effect in Perl like so:#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 */ }
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 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;
It feels like overkill to me. ;)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;
In reply to Re: How to get the name of an aliased function
by japhy
in thread How to get the name of an aliased function
by DrWhy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |