Skip to content

Commit 98f9b49

Browse files
committed
Some more work on S32/IO, WIP
1 parent d25d464 commit 98f9b49

File tree

1 file changed

+144
-122
lines changed

1 file changed

+144
-122
lines changed

S32-setting-library/IO.pod

Lines changed: 144 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,43 @@ increase the performance or maintainability.
2020

2121
=head1 Functions
2222

23-
=head2 print()
24-
X<print()>
23+
In alphabetical order.
2524

26-
sub print(*@text --> Bool) is export
25+
=head2 chdir()
26+
X<chdir()>
2727

28-
Print the given text (as C<Str> objects) on $*OUT. Return whether successful.
28+
sub chdir(Str(Any) $dir, Str(Any) $CWD = $*CWD --> Bool) is export
2929

30-
=head2 say()
31-
X<say()>
30+
Changes the current working directory to the given directory, for the scope in
31+
which C<$*CWD> is active. Optionally takes a second positional parameter to
32+
indicate from which directory any relative path specified in the first
33+
positional parameter, should be taken. Returns C<True> if successful, or an
34+
appropriate C<Failure>.
3235

33-
sub say(*@text --> Bool) is export
36+
Please note that this directory has B<no> connection with whatever the
37+
operating system thinks is the current working directory. The value of
38+
C<$*CWD> just will always be prepended to any relative paths in any file
39+
operation in Perl 6.
3440

35-
Print the given text, followed by a new line C<"\n"> on C<$*OUT>. Before
36-
printing, call the C<.gist> method on any non-C<Str> objects. Return whether
37-
successful.
41+
Normal usage:
3842

39-
=head2 note()
40-
X<note()>
43+
chdir("foo");
4144

42-
sub note(*@text --> Bool) is export
45+
A typically lexical scope bound use case:
4346

44-
Print the given text, followed by a new line C<"\n"> on C<$*ERR>. Before
45-
printing, call the C<.gist> method on any non-C<Str> objects. Return whether
46-
successful.
47+
{
48+
my $*CWD;
49+
chdir("foo");
50+
# working directory changed to "foo"
51+
}
52+
# restored to what it was
53+
54+
But for the latter, you're probably better off using L</indir()>.
4755

4856
=head2 dd()
4957
X<dd()>
5058

51-
sub dd(@vars --> Bool) is export
59+
sub dd(@vars --> Bool) is export
5260

5361
Tiny Data Dumper. Takes the C<variables> specified and C<note>s them (on
5462
C<$*ERR>) in an easy to read format, along with the C<name> of the variable.
@@ -57,13 +65,92 @@ So:
5765
my $a = 42;
5866
dd($a); # notes "$a = 42"
5967

60-
=head2 prompt()
61-
X<prompt()>
68+
Returns C<True> (not that that will matter most of the time).
6269

63-
sub prompt($msg --> Bool) is export
70+
=head2 dir()
71+
X<dir()>
6472

65-
Simple Prompter. Print message (as C<Str> object) on C<$*OUT> and obtains a
66-
single line of input from C<$*IN>.
73+
sub dir(
74+
Str(Any) $dir = $*CWD,
75+
Str(Any) $from = $*CWD,
76+
Mu :$test = (exclude . and ..),
77+
Bool :$Str = False,
78+
--> List ) is export
79+
80+
Returns a lazy list of objects doing the C<IO::Pathy> role for the directory
81+
entries in the given the C<$dir>. Optionally takes a second positional
82+
parameter to be used as the root for any relative path specified as the first
83+
positional. Defaults to the directory pointed to by C<$*CWD>.
84+
If dir() fails, it returns an L<X::IO::Dir|S32::Exception/X::IO::Dir> failure.
85+
The following named parameters are optional:
86+
87+
=over 4
88+
89+
=item :test
90+
91+
Expression against which to smart-match for inclusion in result list. By
92+
default excludes C<"."> and C<".."> only.
93+
94+
=item :Str
95+
96+
Boolean indicating to return absolute C<Str>ings, rather than objects doing
97+
the C<IO::Pathy> role. False by default.
98+
99+
=back
100+
101+
=head2 indir()
102+
X<indir()>
103+
104+
sub indir(Str(Any) $dir, &what, Str(Any) $CWD = $*CWD) is export
105+
106+
Change to the directory indicated by the first positional, and execute the
107+
code given by the second positional. Optionally specify the directory to
108+
which any relative dir should be applied. Returns whatever the code returns.
109+
110+
=head2 mkdir()
111+
X<mkdir()>
112+
113+
multi sub mkdir(Str(Any) $dir, Int $mode = 0o777 --> Bool)
114+
115+
Create directory indicated by the first positional, with the (optional) mode
116+
as the second positional. Return True if successful, or an appropriate
117+
C<Failure>.
118+
119+
multi sub mkdir(Int $mode, *@dir --> List)
120+
121+
Use the first positional parameter C<Int> value as mode with which to create
122+
the directories specified with the other positional parameters. Returns a
123+
C<List> with the directories that were successfully created.
124+
125+
=head2 move()
126+
X<move()>
127+
128+
sub move(
129+
Str(Any) $src,
130+
Str(Any) $dest,
131+
Bool :$createonly,
132+
--> Bool ) is export
133+
134+
Moves a file, as indicate by the first positional parameter, by copying its
135+
contents to the destination specified, and then removing the file at the
136+
original location. If the destination is a directory, moves the file into
137+
that directory. If :createonly is set to True, the move fails if a
138+
directory entry already exists at the destination. Returns C<True> upon
139+
success, or an appropriate C<Failure> if the operation could not be completed.
140+
141+
Please use L</rename()> if a file can be moved by renaming (which is usually
142+
possible if the destination is on the same different physical storage device).
143+
Alternately, the C<move()> function is free to try the C<rename()> first,
144+
and if that (silently) fails, do it the hard way.
145+
146+
=head2 note()
147+
X<note()>
148+
149+
sub note(*@text --> Bool) is export
150+
151+
Print the given text, followed by a new line C<"\n"> on C<$*ERR>. Before
152+
printing, call the C<.gist> method on any non-C<Str> objects. Return whether
153+
successful.
67154

68155
=head2 open()
69156
X<open()>
@@ -136,46 +223,38 @@ C<.lines> and C<.get>. Defaults to C<True>.
136223

137224
=back
138225

139-
=head2 dir()
140-
X<dir()>
141-
142-
sub dir($directory as Str = $*CWD,
143-
Mu :$test = (exclude . and ..)
144-
Bool :$Str = False,
145-
:$CWD = $*CWD.Str,
146-
--> List ) is export
147-
148-
Returns a lazy list directory entries in given the C<$directory> as objects
149-
doing the C<IO::Pathy> role. Defualts to the directory pointed to by C<$*CWD>.
150-
If dir() fails, it returns an L<X::IO::Dir|S32::Exception/X::IO::Dir> failure.
151-
The following named parameters are optional:
226+
=head2 print()
227+
X<print()>
152228

153-
=over 4
229+
sub print(*@text --> Bool) is export
154230

155-
=item :test
231+
Print the given text (as C<Str> objects) on $*OUT. Return whether successful.
156232

157-
Expression against which to smart-match for inclusion in result list. By
158-
default excludes C<"."> and C<".."> only.
233+
=head2 prompt()
234+
X<prompt()>
159235

160-
=item :Str
236+
sub prompt(Str(Any) $msg --> Str) is export
161237

162-
Boolean indicating to return absolute C<Str>ings, rather than objects doing
163-
the C<IO::Path> role. False by default.
238+
Simple Prompter. Print message (as C<Str> object) on C<$*OUT> and obtains a
239+
single line of input from C<$*IN>, which is returned.
164240

165-
=item :CWD
241+
=head2 say()
242+
X<say()>
166243

167-
The directory to pre-pend to C<$directory> if specified as a relative path.
168-
Defaults to C<$*CWD>.
244+
sub say(*@text --> Bool) is export
169245

170-
=back
246+
Print the given text, followed by a new line C<"\n"> on C<$*OUT>. Before
247+
printing, call the C<.gist> method on any non-C<Str> objects. Return whether
248+
successful.
171249

172250
=head2 slurp()
173251
X<slurp()>
174252

175-
sub slurp ($what = $*ARGFILES,
176-
Bool :$bin = False,
177-
Str :$enc = "Unicode",
178-
--> Str|Buf ) is export
253+
sub slurp(
254+
$what = $*ARGFILES, # XXX need to generalize $*ARGFILES behaviour
255+
Bool :$bin = False,
256+
Str :$enc = "Unicode",
257+
--> Str|Buf ) is export
179258

180259
Slurps the contents of the entire file into a C<Str> (or C<Buf> if C<:bin>).
181260
Accepts C<:bin> and C<:enc> optional named parameters, with the same meaning
@@ -185,11 +264,11 @@ directory.
185264
=head2 spurt()
186265
X<spurt()>
187266

188-
sub spurt ($where, $what,
189-
Str :$enc = $*ENC,
190-
Bool :append = False,
191-
Bool :$createonly = False,
192-
--> Bool ) is export
267+
sub spurt ($where, $what,
268+
Str :$enc = $*ENC,
269+
Bool :$append = False,
270+
Bool :$createonly = False,
271+
--> Bool ) is export
193272

194273
Writes the indicated contents (2nd positional parameter) to the location
195274
indicated by the first positional parameter (which can either be a string,
@@ -219,21 +298,6 @@ C<False>.
219298

220299
=back
221300

222-
=head2 mkdir()
223-
X<mkdir()>
224-
225-
multi sub mkdir($dir, Int $mode = 0o777 --> Bool)
226-
227-
Create directory indicated by the first positional, with the (optional) mode
228-
as the second positional. Return True if successful, or an appropriate
229-
C<Failure>.
230-
231-
multi sub mkdir(Int $mode, *@dir --> List)
232-
233-
Use the first positional parameter C<Int> value as mode with which to create
234-
the directories specified with the other positional parameters. Returns a
235-
C<List> with the directories that were successfully created.
236-
237301
=head2 rmdir()
238302
X<rmdir()>
239303

@@ -242,48 +306,16 @@ X<rmdir()>
242306
Removes the (empty) directory as indicated by the positional parameter.
243307
Returns C<True> on success or an appropriate C<Failure>.
244308

245-
=head2 chdir()
246-
X<chdir()>
247-
248-
sub chdir($dir as IO, $CWD = $*CWD,
249-
:$test = <d r>
250-
--> Bool) is export
251-
252-
Changes the current working directory to the given directory, for the scope in
253-
which C<$*CWD> is active (if no second positional parameter is given) or for
254-
the scope of the indicated localized C<$*CWD>. A typical use case:
255-
256-
{
257-
chdir("foo", my $*CWD);
258-
# working directory changed to "foo"
259-
}
260-
# restored to what it was
261-
262-
Returns C<True> if successful, or an appropriate C<Failure>, e.g if the
263-
directory does not exist, or is not a directory, or is not readable.
264-
265-
Please note that this directory has B<no> connection with whatever the
266-
operating system thinks is the current working directory. The value of
267-
C<$*CWD> just will always be prepended to any relative paths in any file
268-
operation in Perl 6.
269-
270-
Also note that you can use C<chdir> to set similar dynamic variables, like
271-
C<$*TMPDIR> and C<$*HOME> this way:
272-
273-
chdir("bar", my $*TMPDIR); # set $*TMPDIR in this scope
274-
chdir("bar", my $*HOME); # set $*HOME in this scope
275-
276309
=head2 copy()
277310
X<copy()>
278311

279-
sub copy ($source as IO, $dest as IO,
280-
:$createonly = False,
281-
--> Bool ) is export
312+
sub copy(Str(Any) $src, Str(Any) $dst, :$createonly --> Bool) is export
282313

283314
Copies a file, as indicate by the first positional parameter, to the
284-
destination specified. If :createonly is set to True, copy fails if a file
285-
already exists in the destination. Returns C<True> upon success, or an
286-
appropriate C<Failure> if the operation could not be completed.
315+
destination specified. Optionally takes a named parameter C<:createonly>:
316+
if set to True, the copy will fail if a directory entry already exists at the
317+
destination. Returns C<True> upon success, or an appropriate C<Failure> if
318+
the operation could not be completed.
287319

288320
=head2 rename()
289321
X<rename()>
@@ -300,23 +332,13 @@ an appropriate C<Failure> if the operation could not be completed.
300332
Please use L</move()> if a file could not be moved by renaming (usually because
301333
the destination is on a different physical storage device).
302334

303-
=head2 move()
304-
X<move()>
335+
=head2 tmpdir()
336+
X<tmpdir()>
305337

306-
sub move ($source as IO, $dest as IO,
307-
:$createonly = False,
308-
--> Bool ) is export
309-
310-
Moves a file, as indicate by the first positional parameter, by copying its
311-
contents to the destination specified, and then removing the file at the
312-
original location. If :createonly is set to True, the move fails if a file
313-
already exists in the destination. Returns C<True> upon success, or an
314-
appropriate C<Failure> if the operation could not be completed.
338+
sub tmpdir(Str(Any) $dir, Str(Any) $CWD = $*CWD --> Bool) is export
315339

316-
Please use L</rename()> if a file can be moved by renaming (which is usually
317-
possible if the destination is on the same different physical storage device).
318-
Alternately, the C<move()> function is free to try the C<rename()> first,
319-
and if that (silently) fails, do it the hard way.
340+
Similar in functionality to L</chdir()>, but instead sets the dynamic
341+
variable C<$*TMPDIR> (rather than $*CWD).
320342

321343
=head2 unlink()
322344
X<unlink()>

0 commit comments

Comments
 (0)