I was wondering if there had been any discussion about how to type
file and directory names in Perl 6. I've read a couple of posts about
file test operators, where some have suggested making filenames
special, either as a subtype of Str or something else entirely. That
way Str wouldn't have all these file test methods, which is good
because not all strings are valid filenames.

I know that there are a lot of uncertainties with regard to the file
system, e.g. that you can only truly know that you'll be able to write
to a file when you actually go ahead and try it, so keeping state
about such things is pointless. But having a type or role that
encapsulates the /name/ of a file or a directory might still be
useful. With Perl 5, if you want to do it right, you construct proper
filenames with catfile and catdir (from File::Spec), but it's still
just a string.

Imagine two roles, Filename and Dirname (or Path::File / Path::Dir). I
chose two instead of just one Path role because I think there might be
ambiguities somewhere (same reason File::Spec has both catdir and
catfile), but I might be wrong. Anyway, it would be neat if you could
quickly construct a Str that does one of those roles in the following

    # bin/perl on Unix
    my $rel = qf/usr bin perl/;

    # /usr/bin/perl
    my $abs = qf[/usr bin perl];

    # My Dir With Spaces/subdir
    my $rel_dir = qd/'My Dir With Spaces' subdir/;

(qf is already used to interpolate &function calls, but ignore that
for the moment). The idea here is to have spaces substituted for
whatever delimiter the underlying operating system uses, and it
respects quotes (like «» does) for when a path component has spaces.

Your everyday cross-platform file test would then look something like this:

unless qf/foo logfile.txt/.exists {
    # complain

The roles might have an .open method which returns a filehandle, and
some other conveniences (e.g. a .splitpath method for files and
.splitdir for dirs).

Reply via email to