Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent semantics for slurp and lines subs #947

Closed
p6rt opened this issue Apr 24, 2009 · 8 comments
Closed

Consistent semantics for slurp and lines subs #947

p6rt opened this issue Apr 24, 2009 · 8 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Apr 24, 2009

Migrated from rt.perl.org#65120 (status was 'rejected')

Searchable as RT65120$

@p6rt
Copy link
Author

p6rt commented Apr 24, 2009

From @ronaldxs

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.

@p6rt
Copy link
Author

p6rt commented Apr 24, 2009

From @ronaldxs

0001-Provide-semantics-for-slurp-filehandle-consistent-w.patch
From 0c032d4a8b9f14effe0047fb3dc6cadb3cbcbb0d Mon Sep 17 00:00:00 2001
From: U-ron-PC\ron <ron@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

@p6rt
Copy link
Author

p6rt commented Apr 24, 2009

From @ronaldxs

S16-unfiled-slurp.patch
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

@p6rt
Copy link
Author

p6rt commented Jul 28, 2010

From @coke

On Fri Apr 24 15​:02​:46 2009, ronaldxs wrote​:

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.

Thanks for the patch ... unfortunately, this patch no longer cleanly applies. Can you rebase
and re-submit? I'll leave the ticket open for a little while.

Regards.

--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Jul 28, 2010

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Sep 11, 2011

From @ronaldxs

Thanks for the patch ... unfortunately, this patch no longer cleanly
applies. Can you rebase
and re-submit? I'll leave the ticket open for a little while.

Regards.

I don't know how long this has been true, but at this point $*IO.slurp
and $fh.slurp work and that seems good enough. Propose closing this
ticket as resolved shortly if no objections.

Ron

@p6rt
Copy link
Author

p6rt commented Sep 11, 2011

From @masak

On Sun Sep 11 10​:14​:08 2011, ronaldxs wrote​:

Thanks for the patch ... unfortunately, this patch no longer cleanly
applies. Can you rebase
and re-submit? I'll leave the ticket open for a little while.

Regards.

I don't know how long this has been true, but at this point $*IO.slurp
and $fh.slurp work and that seems good enough. Propose closing this
ticket as resolved shortly if no objections.

Thanks again for the patches. Sorry about not acting fast enough for them
to be applied.

Rejecting ticket.

@p6rt
Copy link
Author

p6rt commented Sep 11, 2011

@masak - Status changed from 'open' to 'rejected'

@p6rt p6rt closed this as completed Sep 11, 2011
@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant