thor has asked for the wisdom of the Perl Monks concerning the following question:

Greetings all,

I'm writing a module and found myself debating as to what to call one of the methods. Neither of the two names was better, so I want to provide both and let the coder choose which s/he likes. Now, I've seen a couple of ways to do this. Assume that we already have a method called "foo" that we want to have an alias of "bar":

*bar = \&foo; #has always seemed a bit heavy handed to me sub bar { return(foo(@_)); #is there something hidden and wrong here? seems too simple... } #others?
I don't really have a preference, but I don't want to seem like a dolt, either. How do you treat this situation when you encounter it?

thor

Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come

Replies are listed 'Best First'.
Re: Current en vogue technique for aliasing subs
by GrandFather (Saint) on Aug 29, 2005 at 22:33 UTC

    In this case the real answer is: Choose a name. For purposes of documention, maintenance and name space pollution you are better to have only one way of accessing identical functionality.


    Perl is Huffman encoded by design.

      Tell that to the maintainer/author of XML::Twig. ;-) The answer there was: depends on how you look at your data - XML-ish or SGML-ish? The same behaviour has different terminologies between the two, and you can use the terminology that fits your problem space.

      Although you may have two identical functionalities, they may only be identical in expression, while quite different in semantics.

      Or, thor may just be fretting over nothing - sometimes there is no "right" or "wrong" answer, it's just worth making a decision and sticking to it, and it'll be as good as the other choice, and likely few, if any, will complain anyway. ;-)

Re: Current en vogue technique for aliasing subs
by Tanktalus (Canon) on Aug 29, 2005 at 22:32 UTC

    Using import? ;-) Personally, I always go for the former choice over the latter. The latter is great for closures and stuff, but the former has a distinct simplicity that makes it quite explicit that I'm making an alias, end of story. I suspect that the former is also faster in that it's one less function call on the stack, but that's usually a secondary concern, if a concern at all.

      the former is also faster in that it's one less function call on the stack, but that's usually a secondary concern, if a concern at all.

      Well, yes, usually the extra stack work wouldn't amount to much, unless/until the alias in question ends up getting called two million times on a given data set...

      Wouldn't that be a fun and enlightening exercise in benchmarking? Someone uses the same module with the same test code on the same data set, but just switches between the "alias" name and the "real" name of the function, and gets to see the impact of the extra stack layer. (But I can imagine that some folks would feel like they had better things to do with their time.)

Re: Current en vogue technique for aliasing subs
by ysth (Canon) on Aug 29, 2005 at 23:40 UTC
    *bar = \&foo; #has always seemed a bit heavy handed to me
    Can you describe what seems "a bit heavy handed" about it?
Re: Current en vogue technique for aliasing subs
by InfiniteSilence (Curate) on Aug 29, 2005 at 22:49 UTC
    Well, I can think of *one* reason why you might not want to do this:
    U:\>perl -e "package bar; sub mankato {print __PACKAGE__}; package foo +; *mankato = \&bar::mankato; mankato();" bar
    Update: I guess a slightly better example would be:
    U:\>perl -we "package bar; sub new {return(bless({}))}; sub mankato{pr +int __PACKAGE__}; package foo; push @ISA, qw|bar|; package main; my $ +o = foo->new(); $o->mankato();" bar

    Celebrate Intellectual Diversity

Re: Current en vogue technique for aliasing subs
by polettix (Vicar) on Aug 30, 2005 at 10:36 UTC
    Another option could involve using goto:
    sub bar { goto &foo }
    but you should benchmark it against your 'simple' solution to see if it's better. But I wouldn't mind being heavy-handed if you clearly document it with a comment, or group all the aliasing in a dedicated section of your code.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.