Files and Filesystems
File path components are separated with
/
on Unix, with \
on Windows,
and with : on Macs. Some systems support neither hard links
(link
) nor symbolic links
(symlink
, readlink
,
lstat
). Some systems pay attention to
capitalization of filenames, some don't, and some pay attention when
creating files but not when reading them.
There are modules that can help. The standard
File::Spec
modules provide some functions of the
Right Thing persuasion:
use File::Spec::Functions; chdir( updir() ); # go up one directory $file = catfile( curdir(), 'temp', 'file.txt' );
That last line reads in ./temp/file.txt on
Unix and Windows, or :temp:file.txt on Macs, or
[.temp]file.txt on VMS, and stores the file's
contents in $file
.
The File::Basename
module, another
platform-tolerant module bundled with Perl, splits a pathname into its
components: the base filename, the full path to the directory, and the
file suffix.
Here are some tips for writing portable file-manipulating Perl programs:
Don't use two files of the same name with different case, like test.pl and Test.pl, since some platforms ignore capitalization.
Constrain filenames to the 8.3 convention (eight-letter names and three-letter extensions) where possible. You can often get away with longer filenames as long as you make sure the filenames will remain unique when shoved through an 8.3-sized hole in the wall. (Hey, it's gotta be easier than shoving a camel through the eye of a needle.)
Minimize nonalphanumeric characters in filenames. ...
Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.