How can I construct OS-independent file paths in Perl including an optional Windows drive letter?

path, perl, windows

Solution

You want File::Spec's `catpath`:

       catpath()
         Takes volume, directory and file portions and returns an entire path.
         Under Unix, $volume is ignored, and directory and file are
         concatenated.  A '/' is inserted if need be.  On other OSes, $volume
         is significant.

             $full_path = File::Spec->catpath( $volume, $directory, $file );

Problem

I need to construct a file path inside a Perl script. Which path separator should I use to allow my script to work on both Windows and Unix? Keep in mind that Windows needs a drive letter.

Original source