Skip to content

Commit cdc701e

Browse files
committed
[io grant] Add "What's an IO::Path Anyway?" section to TDIOG
1 parent dfdd845 commit cdc701e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

doc/Language/io-guide.pod6

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ functional programming style or in Perl 6 one liners.
2929
While L<IO::Socket> and its subclasses also have to do with Input and Output,
3030
this guide does not cover them.
3131
32+
=head1 Navigating Paths
33+
34+
=head2 What's an IO::Path Anyway?
35+
36+
To represent paths to either files or directories, use L<IO::Path> type.
37+
The simplest way to obtain an object of that type is to coerce a L<Str> by
38+
calling L«C<.IO>|/routine/IO» method on it:
39+
40+
say 'my-file.txt'.IO; # OUTPUT: «"my-file.txt".IO␤»
41+
42+
It may seem like something is missing here—there is no volume or absolute
43+
path involved—but that information is actually present in the object. You can
44+
see it by using L«C<.perl>|/routine/perl» method:
45+
46+
say 'my-file.txt'.IO.perl;
47+
# OUTPUT: «IO::Path.new("my-file.txt", :SPEC(IO::Spec::Unix), :CWD("/home/camelia"))␤»
48+
49+
The two extra attributes—C<SPEC> and C<CWD>—specify what type of operating
50+
system semantics the path should use as well as the "current working directory"
51+
for the path, i.e. if it's a relative path, then it's relative to that
52+
directory.
53+
54+
This means that regardless of how you made one, an L<IO::Path> object
55+
technically always refers to an absolute path. This is why its
56+
L«C<.absolute>|/routine/absolute» and L«C<.relative>|/routine/relative»
57+
methods return L<Str> objects and they are the correct way to stringify a path.
58+
59+
However, don't be in a rush to stringify anything. Pass paths around as
60+
L<IO::Path|/type/IO::Path> objects. All the routines that operate on paths
61+
can handle them, so there's no need to convert them.
62+
3263
=head1 The Wrong Way To Do Things
3364
3465
This section describes how NOT to do Perl 6 IO.

0 commit comments

Comments
 (0)