Alex Rivera | Logout

Aliasing a function in Perl

Asked 2010-12-22T17:54:54.180
18

In Perl, if I want foo() to do exactly what bar() does, I can do this:

sub foo {return bar(@_);} 

Is there a better way? Something closer to Ruby's "alias" operator?

Edit
Report

1 Answer

12
*foo = \&bar;

Assigning a reference to a glob replaces that portion of the glob with the thingy referred to. So this makes &foo exactly the same as &bar.

*foo = *bar;  # or \*bar

This also works, but also aliases the scalar, array, hash, filehandle, and format between foo and bar.

answered 2010-12-22T17:59:30.780

Your Answer