in reply to I humbly ask for your help with parsing

I think File::Spec's catdir(), splitdir(), catpath(), and splitpath() functions may be what you are looking for. You may use splitpath() to break the name apart, splitdir() to break the directory into a list, push() the desired directory name onto the end, then catdir() to reassemble the directory path, then catpath() to build the full name.

Hope that helps.

  • Comment on Re: I humbly ask for your help with parsing

Replies are listed 'Best First'.
Re^2: I humbly ask for your help with parsing
by marinersk (Priest) on Oct 30, 2013 at 22:42 UTC
    ++ atcroft -- File::Specdoes the job:

    #!/usr/bin/perl use strict; use warnings; use File::Spec; { my $pathName = "/usr/tmp/booger/test.dat"; my $baseName = ExtractLastDir($pathName); print "Result is $baseName\n"; } exit; sub ExtractLastDir { my ($fullName, @extraStuff) = @_; if (!defined $fullName) { $fullName = ''; } my ($volumeName, $directoryPath, $fileName) = File::Spec->splitpat +h($fullName); my @directoryNames = File::Spec->splitdir($directoryPath); # Grab the last element from the directory -- but it is probably b +lank my $lastDirectory = pop @directoryNames; # So if it is blank, get the next one if ($lastDirectory eq '') { $lastDirectory = pop @directoryNames; } return $lastDirectory; } __END__ C:\Steve\Dev\PerlMonks\P-2013-10-30@1354-BaseDir>basedir3.pl Result is booger