From: "Chas. Owens" <chas.ow...@gmail.com>
> On Tue, Apr 21, 2009 at 21:15, John W. Krahn <jwkr...@shaw.ca> wrote:
> > Kelly Jones wrote:
> >>
> >> I want foo() and bar() to do the same thing. One way to do this:
> >>
> >> sub foo {return bar(@_);}
> >>
> >> Is there a more clever way using \&bar and things like that?
> >
> > $ perl -le'
> > use warnings;
> > use strict;
> >
> > sub bar { print "in sub bar: @_" }
> >
> > bar 1, 2, 3;
> >
> > sub foo { goto &bar }
> >
> > foo 4, 5, 6;
> > '
> > in sub bar: 1 2 3
> > in sub bar: 4 5 6
> >
> snip
> 
> goto &func; replaces the current subroutine with the called one
> in the same way exec replaces the current process.  Another
> solution is to actually alias the two functions:
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> sub foo {
>       print join(", ", @_), "\n";
> }
> 
> *bar = *foo;

This aliases not only the subroutines, but also (package) variables 
$bar/$foo, @bar/@foo and %bar/%foo (and a few more things). It 
doesn't affect the lexical (declared with my ()) variable though.

If you want to alias just the subroutines use

 *bar = \&foo;

instead.

Jenda
===== je...@krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to