Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

basename & dirname in Perl (See related posts)

These two Perl functions implement approximations of the UNIX utilities `basename` and `dirname`, though basename() automatically strips off the last extension no matter what.
sub basename($) {
 my $file = shift;
 $file =~ s!^(?:.*/)?(.+?)(?:\.[^.]*)?$!$1!;
 return $file;
}

sub dirname($) {my $file = shift; $file =~ s!/?[^/]*/*$!!; return $file; }

Comments on this post

numberwhun posts on Sep 04, 2007 at 12:04
That is definitely an interesting way of doing this while not using any modules. Just as a reference, you can use the File::Basename (http://search.cpan.org/~nwclark/perl-5.8.8/lib/File/Basename.pm) module off of CPAN, which works marvelously. Plus, it is part of the default Perl distribution so its on every machine.

You need to create an account or log in to post comments to this site.


Click here to browse all 4862 code snippets

Related Posts