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";
In reply to Re: aliasing subs
by edan
in thread aliasing subs
by december
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |