Permalink
Browse files

[IO] implement binary slurp

also coerce filename to Str before unboxing. Should avoid the LTA error message
reported in RT #114124
  • Loading branch information...
1 parent 74e1838 commit 23d186c3e39eb43fd9adc1ca5b38af72e53f50b1 @moritz moritz committed Jul 14, 2012
Showing with 19 additions and 6 deletions.
  1. +19 −6 src/core/IO.pm
View
@@ -96,7 +96,7 @@ class IO does IO::FileTestable {
nqp::bindattr(self, IO, '$!PIO',
$path eq '-'
?? ( $w || $a ?? pir::getstdout__P() !! pir::getstdin__P() )
- !! nqp::open(nqp::unbox_s($path), nqp::unbox_s($mode))
+ !! nqp::open(nqp::unbox_s($path.Str), nqp::unbox_s($mode))
);
$!path = $path;
$!chomp = $chomp;
@@ -354,12 +354,25 @@ multi sub close($fh) {
}
proto sub slurp(|$) { * }
-multi sub slurp($filename) {
- my $handle = open($filename, :r);
- my $contents = $handle.slurp();
- $handle.close();
- $contents
+multi sub slurp($filename, :$bin = False) {
+ my $handle = open($filename, :r, :$bin);
+ if $bin {
+ my $Buf = Buf.new();
+ loop {
+ my $current = $handle.read(10_000);
+ $Buf ~= $current;
+ last if $current.bytes == 0;
+ }
+ $handle.close;
+ $Buf;
+ }
+ else {
+ my $contents = $handle.slurp();
+ $handle.close();
+ $contents
+ }
}
+
multi sub slurp(IO $io = $*ARGFILES) {
$io.slurp;
}

0 comments on commit 23d186c

Please sign in to comment.