Last active
November 2, 2016 11:38
-
-
Save tbrowder/60914f63ce2b98cd58f40006078ffb41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
use Test; | |
use File::Temp; | |
multi sub dtext(IO::Handle:D $f, Str $str is copy, Bool :$triple = False) { | |
my $s = $str ~ $str; # double the input text | |
$s ~= $str if $triple; # or triple it | |
$f.say: $s; | |
} | |
multi sub dtext(Str:D $str is rw, Bool :$triple = False) { | |
# internal temp file name ($f) and its handle ($fh) | |
my ($f, $fh) = tempfile; # note temp file are opened rw | |
my $s = $str; | |
dtext($fh, $s, :$triple); # double the text | |
# assign the file contents to the incoming text | |
$str = slurp $f; | |
} | |
my $f = 'test-file.txt'; | |
my $sr = 'text'; | |
my $srw = $sr; | |
# expected output: | |
my $res2 = "texttext\n"; | |
my $res3 = "texttexttext\n"; | |
# the default double-text option | |
my $fh = open $f, :w; | |
dtext($fh, $sr); # output to file | |
my $sr-out = slurp $f; | |
dtext($srw); # output to string | |
# try the triple option | |
$fh = open $f, :w; | |
dtext($fh, $sr, :triple); # output to file | |
my $sr3-out = slurp $f; | |
my $srw3 = $sr; | |
dtext($srw3, :triple); # output to string | |
# test all | |
is $sr-out, $res2; | |
is $srw, $res2; | |
is $sr3-out, $res3; | |
is $srw3, $res3; | |
done-testing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment