Skip to content

Commit 052aa28

Browse files
committed
Created a "Type" independent routines
This shows up as "routines" because it's not a class or anything, but does not make htmlify.p6 choke. This closes #2070. Also closes #2578 since we will not be creating that unless this proves down the line to be untenable. Still, #2059 has to be addressed. We have copied routines here, but not eliminated them there.
1 parent 957462d commit 052aa28

File tree

1 file changed

+392
-0
lines changed

1 file changed

+392
-0
lines changed

doc/Type/independent-routines.pod6

Lines changed: 392 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,392 @@
1+
=begin pod
2+
3+
=TITLE Independent routines
4+
5+
=SUBTITLE Routines not defined within any class or role.
6+
7+
These routines are defined in different files, but are not actually attached to
8+
any particular class or role.
9+
10+
=head2 sub mkdir
11+
12+
Defined as:
13+
14+
sub mkdir(IO() $path, Int() $mode = 0o777 --> IO::Path:D)
15+
16+
Creates a new directory; see L«C<mode>|/routine/mode» for explanation and
17+
valid values for C<$mode>. Returns the L<IO::Path> object pointing to
18+
the newly created directory on success;
19+
L<fails|/routine/fail> with L<X::IO::Mkdir> if directory cannot be created.
20+
21+
Also creates parent directories, as needed (similar to *nix utility
22+
C<mkdir> with C<-p> option); that is, C<mkdir "foo/bar/ber/meow"> will
23+
create C<foo>, C<foo/bar>, and C<foo/bar/ber> directories if they do not
24+
exist, as well as C<foo/bar/ber/meow>.
25+
26+
=head2 sub chdir
27+
28+
Defined as:
29+
30+
sub chdir(IO() $path, :$d = True, :$r, :$w, :$x --> IO::Path:D)
31+
32+
Changes value of C<$*CWD> variable to the provided C<$path>, optionally ensuring
33+
the new path passes several file tests. B<NOTE:> that this routine does I<NOT>
34+
alter the process's current directory (see
35+
L«C<&*chdir>|/routine/&*chdir»).
36+
37+
Returns L«C<IO::Path>|/type/IO::Path»
38+
representing new C<$*CWD> on success. On failure, returns
39+
L«C<Failure>|/type/Failure» and leaves C<$*CWD> untouched.
40+
The C<$path> can be any object with an IO method that returns an
41+
L«C<IO::Path>|/type/IO::Path» object. The available file tests are:
42+
43+
=item C<:d> — check L«C<.d>|/routine/d» returns C<True>
44+
45+
=item C<:r> — check L«C<.r>|/routine/r» returns C<True>
46+
47+
=item C<:w> — check L«C<.w>|/routine/w» returns C<True>
48+
49+
=item C<:x> — check L«C<.x>|/routine/x» returns C<True>
50+
51+
By default, only C<:d> test is performed.
52+
53+
=for code
54+
chdir '/tmp'; # change $*CWD to '/tmp' and check its .d is True
55+
chdir :r, :w, '/tmp'; # … check its .r and .w are True
56+
chdir '/not-there'; # returns Failure
57+
58+
Note that the following construct is a mistake:
59+
60+
=for code
61+
# WRONG! DO NOT DO THIS!
62+
my $*CWD = chdir '/tmp/';
63+
64+
Use L«C<indir>|/routine/indir» instead.
65+
66+
=head2 sub &*chdir
67+
68+
Defined as:
69+
70+
=for code
71+
PROCESS::<&chdir> = sub (IO() $path --> IO::Path:D) { }
72+
73+
Changes value of C<$*CWD> variable to the provided C<$path> and sets
74+
the process's current directory to the value of
75+
L«C<$path.absolute>|/routine/absolute». B<NOTE:> that in most cases,
76+
you want to use L«C<chdir>|/routine/chdir» routine instead.
77+
78+
Returns L«C<IO::Path>|/type/IO::Path»
79+
representing new C<$*CWD> on success. On failure, returns
80+
L«C<Failure>|/type/Failure» and leaves C<$*CWD> untouched.
81+
The C<$path> can be any object with an IO method that returns an
82+
L«C<IO::Path>|/type/IO::Path» object.
83+
84+
Note that unlike regular L«C<chdir>|/routine/chdir», there are no arguments
85+
to specify which file tests to perform.
86+
87+
=for code
88+
&*chdir('/tmp'); # change $*CWD and process's current directory to '/tmp'
89+
&*chdir('/not-there'); # returns Failure
90+
91+
Note that the following construct is a mistake:
92+
93+
=for code
94+
# WRONG! DO NOT DO THIS!
95+
my $*CWD = &*chdir('/tmp');
96+
97+
Use the following, instead; or see L«C<indir>|/routine/indir» if
98+
you do not need to change process's current directory:
99+
100+
=for code
101+
temp $*CWD;
102+
&*chdir('/tmp');
103+
104+
=head2 sub chmod
105+
106+
Defined as:
107+
108+
sub chmod(Int() $mode, *@filenames --> List)
109+
110+
Coerces all C<@filenames> to L«C<IO::Path>|/type/IO::Path» and calls
111+
L«C<IO::Path.chmod>|/type/IO::Path#method_chmod» with C<$mode> on them.
112+
Returns a L«C<List>|/type/List» containing a subset of C<@filenames> for which
113+
C<chmod> was successfully executed.
114+
115+
chmod 0o755, <myfile1 myfile2>; # make two files executable by the owner
116+
117+
=head2 sub indir
118+
119+
Defined as:
120+
121+
sub indir(IO() $path, &code, :$d = True, :$r, :$w, :$x --> Mu)
122+
123+
Takes L«C<Callable>|/type/Callable» C<&code> and executes it after locally (to
124+
C<&code>) changing C<$*CWD> variable to an L<IO::Path> object based on C<$path>,
125+
optionally ensuring the new path passes several file tests. If C<$path> is
126+
relative, it will be turned into an absolute path, even if an L<IO::Path>
127+
object was given. B<NOTE:> that this routine does I<NOT> alter the process's
128+
current directory (see L«C<&*chdir>|/routine/&*chdir»). The C<$*CWD>
129+
outside of the C<&code> is not affected, even if C<&code> explicitly assigns
130+
a new value to C<$*CWD>.
131+
132+
Returns the return value of C<&code> on success. On failure to
133+
successfully change C<$*CWD>, returns L«C<Failure>|/type/Failure».
134+
B<WARNING:> keep in mind that lazily evaluated things might end up NOT
135+
having the C<$*CWD> set by C<indir> in their dynamic scope by the time
136+
they're actually evaluated. Either ensure the generators have their
137+
C<$*CWD> set or L<eagerly evaluate|/routine/eager> them before returning
138+
the results from C<indir>:
139+
140+
say indir("/tmp", {
141+
gather { take ".".IO }
142+
})».CWD; # OUTPUT: «(/home/camelia)␤»
143+
144+
say indir("/tmp", {
145+
eager gather { take ".".IO }
146+
})».CWD; # OUTPUT: «(/tmp)␤»
147+
148+
say indir("/tmp", {
149+
my $cwd = $*CWD;
150+
gather { temp $*CWD = $cwd; take ".".IO }
151+
})».CWD; # OUTPUT: «(/tmp)␤»
152+
153+
The routine's C<$path> argument can be any object with an IO method that
154+
returns an L«C<IO::Path>|/type/IO::Path» object. The available file
155+
tests are:
156+
157+
=item C<:d> — check L«C<.d>|/routine/d» returns C<True>
158+
159+
=item C<:r> — check L«C<.r>|/routine/d» returns C<True>
160+
161+
=item C<:w> — check L«C<.w>|/routine/d» returns C<True>
162+
163+
=item C<:x> — check L«C<.x>|/routine/d» returns C<True>
164+
165+
By default, only C<:d> test is performed.
166+
167+
say $*CWD; # OUTPUT: «"/home/camelia".IO␤»
168+
indir '/tmp', { say $*CWD }; # OUTPUT: «"/tmp".IO␤»
169+
say $*CWD; # OUTPUT: «"/home/camelia".IO␤»
170+
171+
indir '/not-there', {;}; # returns Failure; path does not exist
172+
173+
=head2 sub print
174+
175+
Defined as:
176+
177+
multi sub print(**@args --> True)
178+
multi sub print(Junction:D --> True)
179+
180+
Prints the given text on standard output (the
181+
L«C<$*OUT>|/language/variables#index-entry-%24%2AOUT» filehandle), coercing non-L<Str|/type/Str> objects
182+
to L<Str|/type/Str> by calling L«C<.Str> method|/routine/Str». L<Junction|/type/Junction> arguments
183+
L<autothread|/language/glossary#index-entry-Autothreading> and the order of printed strings
184+
is not guaranteed.
185+
186+
print "Hi there!\n"; # OUTPUT: «Hi there!␤»
187+
print "Hi there!"; # OUTPUT: «Hi there!»
188+
print [1, 2, 3]; # OUTPUT: «1 2 3»
189+
190+
To print text and include the trailing newline, use L«C<put>|/type/IO#sub_put».
191+
192+
=head2 sub put
193+
194+
Defined as:
195+
196+
multi sub put(**@args --> True)
197+
multi sub put(Junction:D --> True)
198+
199+
Same as L«C<print>|/type/IO#sub_print», except it uses
200+
L«C<print-nl>|/routine/print-nl» (which prints a L<newline|/language/newline>,
201+
by default) at the end. L<Junction|/type/Junction> arguments
202+
L<autothread|/language/glossary#index-entry-Autothreading> and the order of
203+
printed strings is not guaranteed.
204+
205+
put "Hi there!\n"; # OUTPUT: «Hi there!␤␤»
206+
put "Hi there!"; # OUTPUT: «Hi there!␤»
207+
put [1, 2, 3]; # OUTPUT: «1 2 3␤»
208+
209+
=head2 sub say
210+
211+
Defined as:
212+
213+
multi sub say(**@args --> True)
214+
215+
Prints the "gist" of given objects. Same as L«C<put>|/type/IO#sub_put»,
216+
except uses L«C<.gist>|/routine/gist» method to obtain string
217+
representation of the object.
218+
219+
B<NOTE:> the L«C<.gist>|/routine/gist» method of some objects, such as
220+
L<Lists|/type/List#method_gist>, returns only B<partial> information
221+
about the object (hence the "gist"). If you mean to print textual
222+
information, you most likely want to use L«C<put>|/type/IO#sub_put»
223+
instead.
224+
225+
say Range; # OUTPUT: «(Range)␤»
226+
say class Foo {}; # OUTPUT: «(Foo)␤»
227+
say 'I ♥ Perl6'; # OUTPUT: «I ♥ Perl6␤»
228+
say 1..Inf; # OUTPUT: «1..Inf␤»
229+
230+
=head2 routine note
231+
232+
Defined as:
233+
234+
method note(Mu: -->Bool:D)
235+
multi sub note( --> Bool:D)
236+
multi sub note(Str:D $note --> Bool:D)
237+
multi sub note(**@args --> Bool:D)
238+
239+
Like L«C<say>|/routine/say», except prints output to L«C<$*ERR>|/language/variables#index-entry-%24%2AERR» handle (STDERR).
240+
If no arguments are given to subroutine forms, will use string C<"Noted">.
241+
242+
=begin code
243+
note; # STDERR OUTPUT: «Noted␤»
244+
note 'foo'; # STDERR OUTPUT: «foo␤»
245+
note 1..*; # STDERR OUTPUT: «1..Inf␤»
246+
=end code
247+
248+
=head2 sub prompt
249+
250+
multi sub prompt()
251+
multi sub prompt($msg)
252+
253+
L<Prints|/routine/print> C<$msg> to C<$*OUT> handle if C<$msg> was provided,
254+
then L<gets|/routine/get> a line of input from C<$*IN> handle. By default, this
255+
is equivalent to printing C<$msg> to
256+
L<STDOUT|https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29>,
257+
reading a line from
258+
L<STDIN|https://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29>,
259+
removing the trailing new line, and returning the resultant string. As of Rakudo
260+
2018.08, C<prompt> will create L<allomorphs|/language/numerics#Allomorphs> for
261+
numeric values, equivalent to calling C<val prompt>.
262+
263+
=for code
264+
my $name = prompt "What's your name? ";
265+
say "Hi, $name! Nice to meet you!";
266+
my $age = prompt("Say your age (number)");
267+
my Int $years = $age;
268+
my Str $age-badge = $age;
269+
270+
In the code above, C<$age> will be duck-typed to the allomorph L<IntStr|/type/IntStr> if it's
271+
entered correctly as a number.
272+
273+
=head2 sub open
274+
275+
multi sub open(IO() $path, |args --> IO::Handle:D)
276+
277+
Creates L<a handle|/type/IO::Handle> with the given C<$path>, and calls
278+
L«C<IO::Handle.open>|/type/IO::Handle#method_open», passing any of the
279+
remaining arguments to it. Note that L<IO::Path> type provides numerous
280+
methods for reading and writing from files, so in many common cases you
281+
do not need to C<open> files or deal with L<IO::Handle> type directly.
282+
283+
=begin code
284+
my $fh = open :w, '/tmp/some-file.txt';
285+
$fh.say: 'I ♥ writing Perl code';
286+
$fh.close;
287+
288+
$fh = open '/tmp/some-file.txt';
289+
print $fh.readchars: 4;
290+
$fh.seek: 7, SeekFromCurrent;
291+
say $fh.readchars: 4;
292+
$fh.close;
293+
294+
# OUTPUT: «I ♥ Perl␤»
295+
=end code
296+
297+
=head2 sub slurp
298+
299+
Defined as:
300+
301+
multi sub slurp(IO::Handle:D $fh = $*ARGFILES, |c)
302+
multi sub slurp(IO() $path, |c)
303+
304+
Slurps the contents of the entire file into a C<Str> (or C<Buf> if
305+
C<:bin>). Accepts C<:bin> and C<:enc> optional named parameters, with
306+
the same meaning as L<open()|/routine/open>; possible encodings are the
307+
same as in all the other C<IO> methods and are listed in
308+
L<C<encoding>|/type/IO::Handle#method_encoding> routine. The routine
309+
will C<fail> if the file does not exist, or is a directory. Without any
310+
arguments, sub C<slurp> operates on C<$*ARGFILES>, which defaults to
311+
C<$*IN> in the absence of any filenames.
312+
313+
=begin code
314+
# read entire file as (Unicode) Str
315+
my $text_contents = slurp "path/to/file";
316+
317+
# read entire file as Latin1 Str
318+
my $text_contents = slurp "path/to/file", enc => "latin1";
319+
320+
# read entire file as Buf
321+
my $binary_contents = slurp "path/to/file", :bin;
322+
=end code
323+
324+
=head2 sub spurt
325+
326+
Defined as:
327+
328+
multi spurt(IO() $path, |c)
329+
330+
The C<$path> can be any object with an IO method that returns an
331+
L«C<IO::Path>|/type/IO::Path» object. Calls L«C<IO::Path.spurt>|/routine/spurt»
332+
on the C<$path>, forwarding any of the remaining arguments.
333+
334+
=head3 Options
335+
336+
=item :enc
337+
338+
The encoding with which the contents will be written.
339+
340+
=item :append
341+
342+
Boolean indicating whether to append to a (potentially) existing file. If
343+
the file did not exist yet, it will be created. Defaults to C<False>.
344+
345+
=item :createonly
346+
347+
Boolean indicating whether to fail if the file already exists. Defaults to
348+
C<False>.
349+
350+
=head3 Examples
351+
352+
=begin code
353+
# write directly to a file
354+
spurt 'path/to/file', 'default text, directly written';
355+
356+
# write directly with a non-Unicode encoding
357+
spurt 'path/to/latin1_file', 'latin1 text: äöüß', :enc<latin1>;
358+
359+
spurt 'file-that-already-exists', 'some text'; # overwrite file's contents:
360+
spurt 'file-that-already-exists', ' new text', :append; # append to file's contents:
361+
say slurp 'file-that-already-exists'; # OUTPUT: «some text new text␤»
362+
363+
# fail when writing to a pre-existing file
364+
spurt 'file-that-already-exists', 'new text', :createonly;
365+
# OUTPUT: «Failed to open file /home/camelia/file-that-already-exists: file already exists …»
366+
=end code
367+
368+
=head2 sub shell
369+
370+
sub shell($cmd --> Proc)
371+
372+
Runs a command through the system shell, which defaults to
373+
C<%*ENV<ComSpec> /c> in Windows, C</bin/sh -c> otherwise. All shell meta
374+
characters are interpreted by the shell, including pipes, redirects,
375+
environment variable substitutions and so on. Shell escapes are a severe
376+
security concern and can cause confusion with unusual file names. Use
377+
L<run|/type/Proc#sub_run> if you want to be safe.
378+
379+
The return value is of L<type Proc|/type/Proc>.
380+
381+
shell 'ls -lR | gzip -9 > ls-lR.gz';
382+
383+
See L<Proc|/type/Proc> for more details, for example on how to capture
384+
output.
385+
386+
=head1 Related classes
387+
388+
See also the related classes L<IO::Handle> and L<IO::Path>.
389+
390+
=end pod
391+
392+
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)