13
13
Defined as:
14
14
15
15
= for code :skip-test
16
- method open(IO::Handle:D:
17
- :$bin, :$enc, :$chomp, :$nl-in, Str:D :$nl-out,
18
- Str :$mode,
19
- :$r, :$w, :$a, :$x, :$update, :$rw, :$rx, :$ra,
20
- :$create, :$append, :$truncate, :$exclusive,
21
- --> IO::Handle:D
22
- )
16
+ method open(IO::Handle:D:
17
+ :$bin, :$enc, :$chomp, :$nl-in, Str:D :$nl-out,
18
+ Str :$mode,
19
+ :$r, :$w, :$a, :$x, :$update, :$rw, :$rx, :$ra,
20
+ :$create, :$append, :$truncate, :$exclusive,
21
+ --> IO::Handle:D
22
+ )
23
23
24
24
Opens the handle in one of the modes. L < Fails|/routine/fail > with appropriate
25
25
exception if the open fails.
@@ -40,16 +40,16 @@ The open mode defaults to non-exclusive, read only (same as specifying
40
40
C < :r > ) and can be controlled by a mix of the following arguments:
41
41
42
42
= begin code :skip-test :allow< B R >
43
- :r same as specifying :mode<ro>
43
+ :r same as specifying :mode<ro>
44
44
45
- :w same as specifying :mode<wo>, :create, :truncate
46
- :a same as specifying :mode<wo>, :create, :append
47
- :x same as specifying :mode<wo>, :create, :exclusive
45
+ :w same as specifying :mode<wo>, :create, :truncate
46
+ :a same as specifying :mode<wo>, :create, :append
47
+ :x same as specifying :mode<wo>, :create, :exclusive
48
48
49
- :update same as specifying :mode<rw>
50
- :rw same as specifying :mode<rw>, :create
51
- :ra same as specifying :mode<rw>, :create, :append
52
- :rx same as specifying :mode<rw>, :create, :exclusive
49
+ :update same as specifying :mode<rw>
50
+ :rw same as specifying :mode<rw>, :create
51
+ :ra same as specifying :mode<rw>, :create, :append
52
+ :rx same as specifying :mode<rw>, :create, :exclusive
53
53
= end code
54
54
55
55
Support for combinations of modes I < other > than what is listed above
@@ -61,14 +61,14 @@ to implode, depending on a particular implementation.
61
61
The mode details are:
62
62
63
63
= begin code :skip-test :allow< B R >
64
- :mode<ro> means "read only"
65
- :mode<wo> means "write only"
66
- :mode<rw> means "read and write"
67
-
68
- :create means the file will be created, if it does not exist
69
- :truncate means the file will be emptied, if it exists
70
- :exclusive means .open will fail if the file already exists
71
- :append means writes will be done at the end of file's current contents
64
+ :mode<ro> means "read only"
65
+ :mode<wo> means "write only"
66
+ :mode<rw> means "read and write"
67
+
68
+ :create means the file will be created, if it does not exist
69
+ :truncate means the file will be emptied, if it exists
70
+ :exclusive means .open will fail if the file already exists
71
+ :append means writes will be done at the end of file's current contents
72
72
= end code
73
73
74
74
Attempts to open a directory, write to a handle opened in read-only mode
@@ -135,13 +135,13 @@ L<in binary mode|/type/IO::Handle#method_encoding> will result in
135
135
C < X::IO::BinaryMode > exception being thrown.
136
136
137
137
= begin code
138
- $*IN.get.say; # Read one line from the standard input
138
+ $*IN.get.say; # Read one line from the standard input
139
139
140
- my $fh = open 'filename';
141
- $fh.get.say; # Read one line from a file
142
- $fh.close;
140
+ my $fh = open 'filename';
141
+ $fh.get.say; # Read one line from a file
142
+ $fh.close;
143
143
144
- say get; # Read one line from $*ARGFILES
144
+ say get; # Read one line from $*ARGFILES
145
145
= end code
146
146
147
147
= head2 routine getc
@@ -200,8 +200,8 @@ L«C<.path>|/type/IO::Handle#method_path», if any, the handle is created for
200
200
and whether it is L « C < .opened > |/type/IO::Handle#method_opened» .
201
201
202
202
= begin code
203
- say IO::Handle.new; # IO::Handle<(Any)>(closed)
204
- say "foo".IO.open; # IO::Handle<"foo".IO>(opened)
203
+ say IO::Handle.new; # IO::Handle<(Any)>(closed)
204
+ say "foo".IO.open; # IO::Handle<"foo".IO>(opened)
205
205
= end code
206
206
207
207
= head2 method eof
@@ -231,12 +231,12 @@ vary by implementation and backend. On Rakudo MoarVM the following are
231
231
supported:
232
232
233
233
= for code :skip-test
234
- utf8
235
- utf16
236
- utf8-c8
237
- iso-8859-1
238
- windows-1252
239
- ascii
234
+ utf8
235
+ utf16
236
+ utf8-c8
237
+ iso-8859-1
238
+ windows-1252
239
+ ascii
240
240
241
241
The default encoding is utf8, which undergoes normalization into Unicode B < NFC >
242
242
(normalization form canonical). In some cases you may want to ensure no
@@ -249,13 +249,13 @@ allows aliases C<latin-1> for C<iso-8859-1> encoding and dashed utf versions:
249
249
C < utf-8 > and C < utf-16 > .
250
250
251
251
= begin code
252
- with 'foo'.IO {
253
- .spurt: "First line is text, then:\nBinary";
254
- my $fh will leave {.close} = .open;
255
- $fh.get.say; # OUTPUT: «First line is text, then:»
256
- $fh.encoding: Nil;
257
- $fh.slurp.say; # OUTPUT: «Buf[uint8]:0x<42 69 6e 61 72 79>»
258
- }
252
+ with 'foo'.IO {
253
+ .spurt: "First line is text, then:\nBinary";
254
+ my $fh will leave {.close} = .open;
255
+ $fh.get.say; # OUTPUT: «First line is text, then:»
256
+ $fh.encoding: Nil;
257
+ $fh.slurp.say; # OUTPUT: «Buf[uint8]:0x<42 69 6e 61 72 79>»
258
+ }
259
259
= end code
260
260
261
261
= head2 routine lines
@@ -288,10 +288,10 @@ when you close the handle or attempt to use any other methods that change the
288
288
file position.
289
289
290
290
= begin code
291
- say "The file contains ",
292
- '50GB-file'.IO.open.lines.grep(*.contains: 'Perl').elems,
293
- " lines that mention Perl";
294
- # OUTPUT: «The file contains 72 lines that mention Perl»
291
+ say "The file contains ",
292
+ '50GB-file'.IO.open.lines.grep(*.contains: 'Perl').elems,
293
+ " lines that mention Perl";
294
+ # OUTPUT: «The file contains 72 lines that mention Perl»
295
295
= end code
296
296
297
297
= head2 method lock
@@ -515,11 +515,11 @@ This method can be called even when the handle is not
515
515
L < in binary mode|/type/IO::Handle#method_encoding > .
516
516
517
517
= begin code
518
- (my $file = 'foo'.IO).spurt: 'I ♥ Perl';
519
- given $file.open {
520
- say .read: 6; # OUTPUT: «Buf[uint8]:0x<49 20 e2 99 a5 20>»
521
- .close;
522
- }
518
+ (my $file = 'foo'.IO).spurt: 'I ♥ Perl';
519
+ given $file.open {
520
+ say .read: 6; # OUTPUT: «Buf[uint8]:0x<49 20 e2 99 a5 20>»
521
+ .close;
522
+ }
523
523
= end code
524
524
525
525
= head2 method readchars
@@ -536,11 +536,11 @@ L<in binary mode|/type/IO::Handle#method_encoding> will result in
536
536
C < X::IO::BinaryMode > exception being thrown.
537
537
538
538
= begin code
539
- (my $file = 'foo'.IO).spurt: 'I ♥ Perl';
540
- given $file.open {
541
- say .readchars: 5; # OUTPUT: «I ♥ P»
542
- .close;
543
- }
539
+ (my $file = 'foo'.IO).spurt: 'I ♥ Perl';
540
+ given $file.open {
541
+ say .readchars: 5; # OUTPUT: «I ♥ P»
542
+ .close;
543
+ }
544
544
= end code
545
545
546
546
= head2 method write
@@ -628,15 +628,15 @@ parameter and C<65536> bytes in binary mode or C<65536> characters in non-binary
628
628
mode.
629
629
630
630
= for code
631
- "foo".IO.open(:bin).Supply(:size<10>).tap: *.perl.say;
632
- # OUTPUT:
633
- # Buf[uint8].new(73,32,226,153,165,32,80,101,114,108)
634
- # Buf[uint8].new(32,54,33,10)
631
+ "foo".IO.open(:bin).Supply(:size<10>).tap: *.perl.say;
632
+ # OUTPUT:
633
+ # Buf[uint8].new(73,32,226,153,165,32,80,101,114,108)
634
+ # Buf[uint8].new(32,54,33,10)
635
635
636
- "foo".IO.open.Supply(:size<10>).tap: *.perl.say;
637
- # OUTPUT:
638
- # "I ♥ Perl 6"
639
- # "!\n"
636
+ "foo".IO.open.Supply(:size<10>).tap: *.perl.say;
637
+ # OUTPUT:
638
+ # "I ♥ Perl 6"
639
+ # "!\n"
640
640
641
641
642
642
= head2 method path
@@ -666,7 +666,7 @@ Returns the value of L«C<.path>|/type/IO::Handle#method_path», coerced
666
666
to L < Str > .
667
667
668
668
= for code
669
- say "foo".IO.open.path; # OUTPUT: «"foo".IO»
669
+ say "foo".IO.open.path; # OUTPUT: «"foo".IO»
670
670
671
671
= head2 routine close
672
672
@@ -683,26 +683,26 @@ closing the handles, which ensures the handle is closed regardless of how the
683
683
block is left.
684
684
685
685
= begin code :skip-test
686
- if $do-stuff-with-the-file {
687
- my $fh = open "path-to-file";
688
- LEAVE close $fh;
689
- # ... do stuff with the file
690
- }
686
+ if $do-stuff-with-the-file {
687
+ my $fh = open "path-to-file";
688
+ LEAVE close $fh;
689
+ # ... do stuff with the file
690
+ }
691
691
692
- sub do-stuff-with-the-file (IO $path-to-file)
693
- my $fh = $path-to-file.open;
692
+ sub do-stuff-with-the-file (IO $path-to-file)
693
+ my $fh = $path-to-file.open;
694
694
695
- # stick a `try` on it, since this will get run even when the sub is
696
- # called with wrong arguments, in which case the `$fh` will be an `Any`
697
- LEAVE try close $fh;
695
+ # stick a `try` on it, since this will get run even when the sub is
696
+ # called with wrong arguments, in which case the `$fh` will be an `Any`
697
+ LEAVE try close $fh;
698
698
699
- # ... do stuff with the file
700
- }
699
+ # ... do stuff with the file
700
+ }
701
701
702
- given "foo/bar".IO.open(:w) {
703
- .spurt: "I ♥ Perl 6!";
704
- .close;
705
- }
702
+ given "foo/bar".IO.open(:w) {
703
+ .spurt: "I ♥ Perl 6!";
704
+ .close;
705
+ }
706
706
= end code
707
707
708
708
B < Note: > unlike some other languages, Perl 6 does not use reference counting,
@@ -729,13 +729,13 @@ Will flush the handle, writing any of the buffered data. Returns C<True>
729
729
on success; otherwise, L < fails|/routine/fail > with C < X::IO::Flush > .
730
730
731
731
= begin code :skip-test
732
- given "foo".IO.open: :w {
733
- LEAVE .close;
734
- $fh.print: 'something';
735
- 'foo'.IO.slurp.say; # (if the data got buffered) OUTPUT: «»
736
- $fh.flush; # flush the handle
737
- 'foo'.IO.slurp.say; # OUTPUT: «something»
738
- }
732
+ given "foo".IO.open: :w {
733
+ LEAVE .close;
734
+ $fh.print: 'something';
735
+ 'foo'.IO.slurp.say; # (if the data got buffered) OUTPUT: «»
736
+ $fh.flush; # flush the handle
737
+ 'foo'.IO.slurp.say; # OUTPUT: «something»
738
+ }
739
739
= end code
740
740
741
741
= head2 method native-descriptor
@@ -762,12 +762,12 @@ in routines that C<chomp>, such as L«C<get>|/routine/get» and
762
762
L « C < lines > |/routine/lines» .
763
763
764
764
= begin code
765
- with 'test'.IO {
766
- .spurt: '1foo2bar3foo'; # write some data into our test file
767
- my $fh will leave {.close} = .open; # can also set .nl-in via .open arg
768
- $fh.nl-in = [<foo bar>]; # set two possible line endings to use;
769
- $fh.lines.say; # OUTPUT: ("1", "2", "3").Seq
770
- }
765
+ with 'test'.IO {
766
+ .spurt: '1foo2bar3foo'; # write some data into our test file
767
+ my $fh will leave {.close} = .open; # can also set .nl-in via .open arg
768
+ $fh.nl-in = [<foo bar>]; # set two possible line endings to use;
769
+ $fh.lines.say; # OUTPUT: ("1", "2", "3").Seq
770
+ }
771
771
= end code
772
772
773
773
= head2 method nl-out
@@ -782,15 +782,15 @@ handle, to be used by methods L«C<.put>|/type/IO::Handle#method_put»
782
782
and L « C < .say > |/type/IO::Handle#method_say» .
783
783
784
784
= begin code
785
- with 'test'.IO {
786
- given .open: :w {
787
- .put: 42;
788
- .nl-out = 'foo';
789
- .put: 42;
790
- .close;
791
- }
792
- .slurp.perl.say; # OUTPUT: «"42\n42foo"»
785
+ with 'test'.IO {
786
+ given .open: :w {
787
+ .put: 42;
788
+ .nl-out = 'foo';
789
+ .put: 42;
790
+ .close;
793
791
}
792
+ .slurp.perl.say; # OUTPUT: «"42\n42foo"»
793
+ }
794
794
= end code
795
795
796
796
= head2 method opened
0 commit comments