GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Method::Alias(3) User Contributed Perl Documentation Method::Alias(3)

Method::Alias - Create method aliases (and do it safely)

  # My method
  sub foo {
      ...
  }
  
  # Alias the method
  use Method::Alias 'bar' => 'foo',
                    'baz' => 'foo';

For a very long time, whenever I wanted to have a method alias (provide an alternate name for a method) I would simple do a GLOB alias. That is,

  # My method
  sub foo {
      ...
  }
  
  # Alias the method
  *bar = *foo;

While this works fine for functions, it does not work for methods.

If your class has a subclass that redefines "foo", any call to "bar" will result in the overloaded method being ignored and the wrong "foo" method being called.

These are basically bugs waiting to happen, and having completed a number of very large APIs with lots of depth myself, I've been bitten several times.

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(@_) }

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. I might add this later if someone really wants it, but until then the basic method will suffice.

That doing this right is even worthy of a module is debatable, but I would rather have something that looks like a method alias definition, than have to document additional methods all the time.

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.

  # 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.

Although primarily used as a pragma, you may call import directly if you wish.

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.

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

Adam Kennedy <cpan@ali.as>

<http://ali.as/>

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.

2006-07-15 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.