Skip to content

Commit

Permalink
Improve c<slurp> to be more consistent with Perl6 and work in utf8/bi…
Browse files Browse the repository at this point in the history
…nary mode
  • Loading branch information
bacek committed Jan 6, 2011
1 parent 2f2bc9a commit 412953d
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/setting/IO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,32 @@ IO Methods and Functions

=end

=begin item open
Open file.
=end item

sub open($filename, :$r, :$w, :$a, :$bin) {
my $mode := $w ?? 'w' !! ($a ?? 'wa' !! 'r');
my $handle := pir::new__Ps('FileHandle');
$handle.open($filename, $mode);
$handle.encoding($bin ?? 'binary' !! 'utf8');
$handle;
}

=begin item close
Close handle
=end item

sub close($handle) {
$handle.close();
}

=begin item slurp
Returns the contents of C<$filename> as a single string.
=end item

our sub slurp ($filename) {
my $handle := pir::new__Ps('FileHandle');
$handle.open($filename, 'r');
my $handle := open($filename, :r);
my $contents := $handle.readall;
$handle.close();
$contents;
Expand Down

0 comments on commit 412953d

Please sign in to comment.