/
ch-1.raku
38 lines (30 loc) · 938 Bytes
/
ch-1.raku
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
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-098/
class Read-Chars {
has $!content;
has $!pointer;
submethod TWEAK {
$!content = 'input.txt'.IO.slurp.chomp;
$!pointer = 0;
}
method read-chars ( $chars ) {
my $string = substr($!content,$!pointer,$chars);
$!pointer = $!pointer + $chars;
$!pointer = $!content.chars if $!pointer > $!content.chars;
return $string;
}
}
multi sub MAIN( 'challenge' ) {
my $read = Read-Chars.new;
say 'Input: Suppose the file (input.txt) contains "1234567890"';
say 'Output:';
say "\tprint readN(\"input.txt\", 4): " ~ $read.read-chars(4);
say "\tprint readN(\"input.txt\", 4): " ~ $read.read-chars(4);
say "\tprint readN(\"input.txt\", 4): " ~ $read.read-chars(4);
}
multi sub MAIN( 'test' ) {
use Test;
my $read = Read-Chars.new;
is $read.read-chars(4), '1234';
is $read.read-chars(4), '5678';
is $read.read-chars(4), '90';
}