-1

I am using the File::Spec module like this

my $volume = 'C';

my $path = File::Spec->catpath(
    $volume,
    File::Spec->catdir('panel', 'texts'),
    'file'
);

print $path;

output

Cpanel\texts\file

How is File::Spec a portable module, as discussed in How can I construct OS-independent file paths in Perl ...? if I have to write the volume as C:\ and not just C to get it right?

Community
  • 1
  • 1
Pavel
  • 81
  • 1
  • 7

1 Answers1

6

You have 2 problems. The first is that Windows volume names include the colon, so you should have said $volume = 'C:'. The second is that you specified a relative path, so you got a relative path. If you want an absolute path, you have to give one:

use 5.010;
use File::Spec;

my $volume = 'C:';
my $path = File::Spec->catpath($volume,
    File::Spec->catdir('', 'panel', 'texts'), 'file');
say $path;

On Windows, that will print C:\panel\texts\file, and on Unix it will say /panel/texts/file.

Note that it's perfectly legitimate to have a relative path with a volume name on Windows:

File::Spec->catpath('C:',
    File::Spec->catdir('panel', 'texts'), 'file');

will give you C:panel/texts/file, which means panel/texts/file relative to the current directory on drive C:. (In Windows, each drive has its own current directory.)

ikegami
  • 367,544
  • 15
  • 269
  • 518
cjm
  • 61,471
  • 9
  • 126
  • 175