jjmerelo
jjmerelo

Reputation: 23517

How do we work with OS-independent paths in Perl 6?

Perl 6 does have classes that include common OSs for specifying paths, as well as $*SPEC which contains the file specification. New paths use by default current value of SPEC. However, it's not clear at all, from the documentation, if doing something like

mkdir IO::Path.new( 'a/b/c' );

is going to work correctly across all operating systems, or you need to specifically create OS-dependent code. Any idea?

Upvotes: 7

Views: 111

Answers (2)

Tobias Boege
Tobias Boege

Reputation: 106

I'm trying this on Windows 7 (one of the OSes you seem to be mostly interested in according to tags), using

This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
implementing Perl 6.d.

And it seems to work just like that

> with mkdir IO::Path.new('a/b/c') { say .e; say .absolute }
True
C:\rakudo\a\b\c

The directory is indeed created properly.

Note that IO::Path takes an IO::Spec object defaulting to $*SPEC in its constructor, so the necessary OS-dependent part is available to the object. In Rakudo the IO::Spec is indeed used by mkdir through .absolute. There also is a roast test about / in an IO::Path becoming \ on Windows.

As Elizabeth Mattijsen pointed out, Windows seems to just support forward slashes by itself. Others claim that this has been the case forever:

Actually, every version of Windows, and every version of MS-DOS from 2.0 on, has accepted "/" as a path delimiter.

Upvotes: 9

nxadm
nxadm

Reputation: 2079

On a Windows 10 Enterprise VM:

C:\Users\me>c:/rakudo/bin/perl6 -e "mkdir IO::Path.new( 'a/b/c' )"

C:\Users\me>tree a
Folder PATH listing
Volume serial number is xxx
C:\USERS\ME\A
└───b
    └───c

Upvotes: 4

Related Questions