Use File::Basename to separate a filename from its directory

Question: Using Perl, if I have a string that includes the full path to a file (i.e., it includes both the full path of the directory and the filename), how do I split the string into its directory and filename components?

Answer: Use the basename and dirname methods of the Perl File::Basename module.

The following example shows how to break this string '/Users/al/work/file1.pdf' into its directory and filename components:

A Perl module template (template code for a Perl module)

If you've ever needed some template/boilerplate code for creating your own Perl module, here's some sample code intended to give you just what you wanted. Feel free to copy and paste the Perl code shown below and use it as a template for creating your own modules.

How to use this sample Perl module

If you've never created your own Perl module I recommend first seeing how this sample code works. Here's all you have to do to get it running:

Syndicate content