-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
ArgFiles.pm
49 lines (45 loc) · 1.16 KB
/
ArgFiles.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
my class IO::ArgFiles is IO {
has $.args;
has $.filename;
has $!io;
has $.ins;
method eof() {
! $!args && $!io.opened && $!io.eof
}
method get() {
unless $!io.defined && $!io.opened {
$!filename = $!args ?? $!args.shift !! '-';
$!io = open($!filename, :r) ||
fail "Unable to open file '$!filename'";
}
my $x = $!io.get;
while !$x.defined {
$!io.close;
$!io = IO;
fail "End of argfiles reached" unless $!args;
$x = self.get;
}
$!ins++;
$x;
}
method lines($limit = *) {
my $l = $limit ~~ Whatever ?? $Inf !! $limit;
gather while $l-- > 0 {
take $.get // last;
}
}
method slurp(IO::ArgFiles:D:) {
my @chunks;
if $!io && $!io.opened {
@chunks.push: nqp::p6box_s($!io.readall);
$!io.close;
}
while $!args {
my $fn = $!args.shift;
my $file = open($fn);
@chunks.push: $file.slurp;
}
return $*IN.slurp unless @chunks;
@chunks.join;
}
}