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.
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 );
You want File::Spec. There are specific versions for Unix, Win32, and MacOS as well others.
If you find File::Spec cumbersome, as I do, try Path::Class. It gives you directory and file objects to work with rather than having to call long winded File::Spec class methods on strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With