/
IO.pm
76 lines (61 loc) · 1.5 KB
/
IO.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class IO is also {
multi method close() is export {
try {
?$!PIO.close()
}
$! ?? fail($!) !! Bool::True
}
multi method eof() is export {
?$!PIO.eof();
}
multi method get() is export {
my $x = $!PIO.readline;
return if $.eof && $x eq '';
$!ins++;
$x.chomp;
}
multi method ins() {
$!ins;
}
multi method lines($limit = *) {
my @result;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
while !$.eof && $l-- > 0 {
push @result, $.get;
}
@result;
}
multi method print(*@items) {
try {
for @items -> $item {
$!PIO.print($item);
}
}
$! ?? fail($!) !! Bool::True;
}
multi method printf($format, *@args) {
self.print(sprintf($format, |@args));
}
multi method say(*@items) {
self.print(@items, "\n");
}
multi method slurp() {
$!PIO.readall();
}
}
multi sub lines(IO $filehandle,
:$bin = False,
:$enc = 'Unicode',
:$nl = "\n",
:$chomp = True) {
fail 'Binary mode not supported yet' if $bin;
fail 'Encodings not supported yet' if $enc ne 'Unicode';
fail 'Fancy newlines not supported yet' if $nl ne "\n";
fail 'Lack of chomp not supported yet' if !$chomp;
$filehandle.lines();
}
multi sub prompt($msg) {
print $msg;
$*IN.get;
}
# vim: ft=perl6