11.45. Solution: filename/dirname

Example 11-17. examples/regex/file_basename.pl

#!/usr/bin/perl
use strict;
use warnings;

my $path = "/home/foo/.mozilla/cache/data.txt";

my $filename = ($path =~ m{([^/]*)$} ? $1 : "");
my $dirname  = ($path =~ m{^(.*)/}   ? $1 : "");

# Directory name:

print "$path\n";
print "$filename\n";
print "$dirname\n";





use File::Basename;
print basename($path) . "\n";
print dirname($path) . "\n";



If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed