# New Ticket Created by  Raphael Descamps 
# Please include the string:  [perl #49962]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=49962 >


./perl6 -e 'my $s = "foo"; my $res = chop($s);say $s;say $res'
foo
fo
./perl6 -e 'my $s = "foo"; my $res = $s.chop;say $s;say $res'
foo
fo

I was surprised to discover that chop was still not implemented in
Rakudo. So it's my first try to implement a new perl6 feature and
amazingly, it even seems to works. I still didn't figure out how to
write builtins in perl6, so sorry but it's in PIR for now.

Anyway, it's fun to be able to implement a new perl6 feature so
easily :)
Index: src/builtins/string.pir
===================================================================
--- src/builtins/string.pir	(revision 24981)
+++ src/builtins/string.pir	(working copy)
@@ -200,7 +200,22 @@
     .return ($S0)
 .end
 
+=item chop
 
+ our Str method Str::chop ( Str  $string: )
+
+Returns string with one Char removed from the end.
+
+=cut
+
+.sub 'chop'
+    .param string a
+    .local pmc s
+    s = new 'Perl6Str'
+    s = a
+    .return s.'chop'()
+.end
+
 =back
 
 =head2 TODO Functions
@@ -216,12 +231,6 @@
 list, it chops each item in turn, and returns the last character
 chopped.
 
-=item chop
-
- our Str method Str::chop ( Str  $string: )
-
-Returns string with one Char removed from the end.
-
 =item p5chomp
 
  our Int multi P5emul::Str::p5chomp ( Str  $string is rw )
Index: src/classes/Str.pir
===================================================================
--- src/classes/Str.pir	(revision 24981)
+++ src/classes/Str.pir	(working copy)
@@ -190,7 +190,25 @@
     .return (retv)
 .end
 
+.sub 'chop' :method
+    .local string tmps
+    .local pmc retv
+    .local int len
 
+    retv = new 'Perl6Str'
+    tmps = self
+
+    len = length tmps
+    if len == 0 goto done
+    dec len
+    substr tmps,tmps, 0, len
+  done:
+    retv = tmps
+    .return(retv)
+.end
+
+=cut
+
 # Local Variables:
 #   mode: pir
 #   fill-column: 100

Reply via email to