in reply to Quick Regex Needed

use File::Basename qw( basename ); my $fn = basename($path);

File::Basename is a core module.

Replies are listed 'Best First'.
Re^2: Quick Regex Needed
by graff (Chancellor) on Oct 22, 2008 at 01:07 UTC
    Right, but it's important to know what sort of system you happen to be using, so that you invoke the fileparse_set_fstype() function when necessary:
    $ uname -s Darwin $ echo '\one\two\three\four' \one\two\three\four $ echo '\one\two\three\four' | perl -MFile::Basename -lne 'print basename($_)' \one\two\three\four $ echo '\one\two\three\four' | perl -MFile::Basename -lne 'BEGIN{fileparse_set_fstype("DOS")} prin +t basename($_)' four
    The above would work out the same way if the result of "uname -s" had been linux or unix.

      Only if you assume he wasn't dealing with a path in the first place.

      I assumed the OP was dealing with paths. If he's not, I believe he's better off not using File::Basename at all rather than your workaround.

Re^2: Quick Regex Needed
by Anonymous Monk on Oct 22, 2008 at 01:42 UTC
    With Path::Class, that can be written as
    #!/usr/bin/perl -- use strict; use warnings; use Path::Class; print file(qw' \one\two\three\four ')->basename; print "\n"; __END__ four