# New Ticket Created by Ron Schmidt
# Please include the string: [perl #65120]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=65120 >
Patch provides semantics for "slurp $filehandle" consistent with "slurp
'filename'" and allow "slurp" by itself to slurp stdin. Also provide
consistency with existing lines implementation.
Note that according to Dr. Michaud this patch requires a spec change
which he OKd but I don't feel qualified to make myself. The spec seems
to have slurp and lines subs for file names and methods for file
handles. The implementation already had a lines sub for file handles.
The patch adds a corresponding slurp sub and defaults both slurp and sub
with no arguments to read from stdin/$*IN.
This is my first attempt at submitting a patch using git branching and
the git branch is slurp-sub.
>From 0c032d4a8b9f14effe0047fb3dc6cadb3cbcbb0d Mon Sep 17 00:00:00 2001
From: U-ron-PC\ron <r...@ron-pc.(none)>
Date: Fri, 24 Apr 2009 10:39:20 -0400
Subject: [PATCH] Provide semantics for slurp $filehandle consistent with slurp filename and
allow slurp by itself to slurp stdin. Also provide consistency with
existing lines implementation.
---
src/builtins/io.pir | 10 ----------
src/setting/Any-str.pm | 8 ++++++++
src/setting/IO.pm | 23 +++++++++++++++++++++++
3 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/src/builtins/io.pir b/src/builtins/io.pir
index 1b92857..0c638f5 100644
--- a/src/builtins/io.pir
+++ b/src/builtins/io.pir
@@ -98,16 +98,6 @@ opened_ok:
obj.'close'()
.end
-.sub 'slurp'
- .param string filename
- .local string contents
-
- $P0 = 'open'(filename, 'r')
- contents = $P0.'slurp'()
- 'close'($P0)
- .return(contents)
-.end
-
=item unlink LIST
diff --git a/src/setting/Any-str.pm b/src/setting/Any-str.pm
index b427332..617dcab 100644
--- a/src/setting/Any-str.pm
+++ b/src/setting/Any-str.pm
@@ -173,6 +173,14 @@ multi sub lines(Str $filename,
return lines($filehandle, :$bin, :$enc, :$nl, :$chomp);
}
+multi sub slurp(Str $filename,
+ :$bin = False,
+ :$enc = 'Unicode') {
+
+ my $filehandle = open($filename, :r);
+ return slurp($filehandle, :$bin, :$enc);
+}
+
sub unpack($template, $target) {
$template.trans(/\s+/ => '') ~~ / ((<[Ax]>)(\d+))* /
or return (); # unknown syntax
diff --git a/src/setting/IO.pm b/src/setting/IO.pm
index 0948b6c..6cac0e7 100644
--- a/src/setting/IO.pm
+++ b/src/setting/IO.pm
@@ -62,4 +62,27 @@ multi sub lines(IO $filehandle,
return $filehandle.lines();
}
+multi sub lines(:$bin = False,
+ :$enc = 'Unicode',
+ :$nl = "\n",
+ :$chomp = True) {
+
+ return lines($*IN, :$bin, :$enc, :$nl, :$chomp);
+}
+
+multi sub slurp(IO $filehandle,
+ :$bin = False,
+ :$enc = 'Unicode') {
+
+ fail 'Binary mode not supported yet' if $bin;
+ fail 'Encodings not supported yet' if $enc ne 'Unicode';
+
+ return $filehandle.slurp();
+}
+
+multi sub slurp(:$bin = False,
+ :$enc = 'Unicode') {
+ return slurp($*IN, :$bin, :$enc);
+}
+
# vim: ft=perl6
--
1.6.1.2
Index: t/spec/S16-unfiled/slurp.t
===================================================================
--- t/spec/S16-unfiled/slurp.t (revision 26412)
+++ t/spec/S16-unfiled/slurp.t (working copy)
@@ -2,7 +2,7 @@
use Test;
-plan 4;
+plan 5;
# L<E07/"And every one shall share..." /returns them as a single string/>
# L<S16/"Unfiled"/"=item IO.slurp">
@@ -30,4 +30,16 @@
my @slurped_lines = lines(open($self));
ok +...@slurped_lines > 30, "more than 30 lines in this file ?";
+{
+ my $fh = open $self, :r;
+ my $contents = slurp $fh;
+
+ # lame use of filehandle but might be able to seek/rewind some day ...
+ # also allows 'slurp $*IN' huffmanized to just slurp
+ ok $fh.ins == 0 && $contents ~~ m/'use v6'.*'StringThatsNowhereElse'/,
+ "slurp worked through file handle";
+
+ $fh.close;
+}
+
# vim: ft=perl6