in reply to aliasing subs
I agree with the valid points that were already rasied:
However, my curiosity was piqued about how to go about this, if the need did come up somehow, even though I think solution #2 would be the simplest/best answer most (all?) of the time...
So I came up with this snippet using closures, as you mentioned
for my $wheel (qw/basename dirname/) { no strict 'refs'; *$wheel = sub { my ($dir) = shift; my ($path, $name) = ($dir =~ m{(.*)/([^/]*)$}); if ($wheel eq 'basename') { return $name; } else { return $path; } }; } my $path = dirname('/tmp/foo/bar'); my $name = basename('/tmp/foo/bar'); print "$path/$name\n";
Works for me...
Update: Expunged the OPs leaning toothpicks, since I am attributing the snippet to myself ;-)
Update: Or this:
--for my $funcname (qw/basename dirname/) { no strict 'refs'; *$funcname = sub { my ($dir) = shift; my ($path, $name) = ($dir =~ m{(.*)/([^/]*)$}); if ($funcname eq 'basename') { return $name; } else { return $path; } }; } my $path = dirname('/tmp/foo/bar'); my $name = basename('/tmp/foo/bar'); print "$path/$name\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: aliasing subs
by december (Pilgrim) on May 06, 2003 at 12:16 UTC | |
by Jenda (Abbot) on May 06, 2003 at 17:55 UTC |