Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[IO] implement binary slurp
also coerce filename to Str before unboxing. Should avoid the LTA error message
reported in RT #114124
  • Loading branch information
moritz committed Jul 14, 2012
1 parent 74e1838 commit 23d186c
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/core/IO.pm
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 23d186c

Please sign in to comment.