Permalink
Browse files

Improve c<slurp> to be more consistent with Perl6 and work in utf8/bi…

…nary mode
  • Loading branch information...
1 parent 2f2bc9a commit 412953d1e38ea6e045c76df63a5f216bd35ea37d @bacek bacek committed Jan 6, 2011
Showing with 21 additions and 2 deletions.
  1. +21 −2 src/setting/IO.pm
View
23 src/setting/IO.pm
@@ -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;

0 comments on commit 412953d

Please sign in to comment.