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

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.