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;

foo(1, 2, 3);
bar(1, 2, 3);

You might also want to look on search.cpan.org for modules in the
Sub::* hierarchy.  One of them may give you a better interface.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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