Method::Alias(3pm)
π Method::Alias π
okay bestie, so this is Method::Alias, a lil Perl module (it lives in the User Contributed Perl Documentation section, Method::Alias(3pm), iykyk) and its whole vibe is: Create method aliases (and do it safely) β¨
πΈ SYNOPSIS πΈ
like, imagine you got a method:
# My method
sub foo {
...
}
and you wanna alias it, so you just:
# Alias the method
use Method::Alias 'bar' => 'foo',
'baz' => 'foo';
boom. done. two aliases, zero stress π
π¦ DESCRIPTION π¦
okay so storytime π for a VERY long time, whenever our dear author wanted a method alias (that's just, like, an alternate name for a method), he would simply do a GLOB alias. as in:
# My method
sub foo {
...
}
# Alias the method
*bar = *foo;
and honestly? for functions, that works fine. no notes. but for methods?? absolutely not, bestie, it does NOT work. π
here's the tea: if your class has a subclass that redefines foo, then any call to bar will result in the overloaded method being ignored and the wrong foo method being called. like, the subclass did all that work and got ghosted. rude.
these are basically bugs waiting to happen, and having completed a number of very large APIs with lots of depth himself, the author has been bitten several times. π been there.
so in this situation, the canonical and fasest way to handle an alias looks something like this:
# My method
sub foo {
...
}
# Alias the method
sub bar { shift->foo(@_) }
(and yes, the original really does say "fasest", we keep the typos in this house π«Ά)
note that this adds an extra entry to the caller array, but this isn't really all that important unless you are paranoid about these things. π
the alternative would be to try to find the method using UNIVERSAL::can, and then goto it. the author might add this later if someone really wants it, but until then the basic method will suffice β¨
now, is doing this right even worthy of a module? debatable!! but he would rather have something that looks like a method alias definition, than have to document additional methods all the time. iconic reasoning, honestly. π
β¨ Using Method::Alias β¨
Method::Alias is designed to be used as a pragma, to which you provide a set of pairs of method names. only very minimal checking is done β if you wish to create infinite loops or what have you, you are more than welcome to shoot yourself in the foot. π―π¦Ά (no cap, that's the actual energy of the docs)
# Add a single method alias
use Method::Alias 'foo' => 'bar';
# Add several method aliases
use Method::Alias 'a' => 'b',
'c' => 'd',
'e' => 'f';
and for now, that's all there is to it. periodt. π
π METHODS π
import from => to, ...
although it's primarily used as a pragma, you may call import directly if you wish. no gatekeeping here.
taking a set of pairs of normal strings, the import method creates a number of methods in the caller's package to call the real method.
returns true, or dies on error. π€ (so like, handle your errors, bestie)
π«Ά SUPPORT π«Ά
bugs should always be submitted via the CPAN bug tracker:
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Method-Alias>
for other issues, contact the maintainer π
π AUTHORS π
Adam Kennedy <cpan@ali.as>
π SEE ALSO π
π COPYRIGHT π
Copyright 2004, 2006 Adam Kennedy. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
okay that's the whole page bestie, slay, go alias responsibly π¦β¨ no cap, this page is perl v5.36.0 energy, dated 2022-10-21, and it's giving main character manual page. mwah π π
π¦ where this page comes from
- ubuntu noble: libmethod-alias-perl 1.03-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmethod-alias-perl 1.03-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmethod-alias-perl 1.03-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms