diff options
| -rw-r--r-- | challenge-098/abigail/README.md | 37 | ||||
| -rw-r--r-- | challenge-098/abigail/perl/ch-1.pl | 73 |
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-098/abigail/README.md b/challenge-098/abigail/README.md index 7434a36a21..f7d65c3bb7 100644 --- a/challenge-098/abigail/README.md +++ b/challenge-098/abigail/README.md @@ -6,6 +6,43 @@ You are given file `$FILE`. Create subroutine `readN($FILE, $number)` returns the first `n`-characters and moves the pointer to the (`n+1`)th character. +### Notes + +What a poorly defined challenge. + +> You are given a file + +What does that mean? Do we get the content? A file handle? +A file descriptor? A file name? + +The example (but not the challenge itself), suggest we're getting +a file name. Ok, but....: + +> moves the pointer + +What pointer? File handles/descriptors point to something, so +then it can be argued we should leave the file handle open, +pointing to a place in the file. But we have just established we +are *not getting* a file handle -- we're getting a file name. +So, what pointer are we talking about? + +What on earth are we supposed to do? + +Are we supposed to create a file handle, and keep file handle open? +Should we just slurp in the file content, and keep a pointer +to what we have returned? + +What should happen if we call `readN` with different files, interleaved? +Keep track of where we are for each file? Restart if called with `file1`, +then `file2`, and then `file1` again? + +Our implementation will be a real stab in the dark -- we've no idea what +we are supposed to do. + +### Input +Our program will read lines from standard input; each line consists +of a filename and an amount of characters to read, separated by whitespace. + ### Example ~~~~ Input: Suppose the file (input.txt) contains "1234567890" diff --git a/challenge-098/abigail/perl/ch-1.pl b/challenge-098/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..b0c3a4d0da --- /dev/null +++ b/challenge-098/abigail/perl/ch-1.pl @@ -0,0 +1,73 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# +# The input file will contain, on each line, a file name and an amount. +# + + +# +# What a poorly defined challenge. +# +# "You are given a file". +# +# What does that mean? Do we get the content? A file handle? +# A file descriptor? A file name? +# +# The example (but not the challenge itself), suggest we're getting +# a file name. Ok, but....: +# +# "moves the pointer" +# +# What pointer? File handles/descriptors point to something, so +# then it can be argued we should leave the file handle open, +# pointing to a place in the file. But we have just established we +# are NOT GETTING a file handle -- we're getting a file name. +# So, what pointer are we talking about? +# +# What on earth are we supposed to do? +# +# Are we supposed to create a file handle, and keep file handle open? +# Should we just slurp in the file content, and keep a pointer +# to what we have returned? +# +# What should happen if we call 'readN' with different files, interleaved? +# Keep track of where we are for each file? Restart if called with file1, +# then file2, and then file1 again? +# +# What follows is a real stab in the dark -- I've no idea what +# we are supposed to do. +# + +# +# Given a filename, read its contents if called for the first time with +# that filename. Return the next $amount of characters. (Or less if we +# have exhausted the content of the file). +# +sub readN ($filename, $amount) { + state $cache; + chomp ($$cache {$filename} //= do {local (@ARGV, $/) = $filename; <>}); + substr $$cache {$filename} => 0, $amount, "" +} + +while (<STDIN>) { + chomp; + my ($filename, $amount) = split ' '; + say readN $filename, $amount; +} + +__END__ |
