Permalink
Browse files

Make $scalar->reverse always reverse $scalar, regardless of context

In list context, this previously reversed the list ($scalar), thus
doing nothing. Now, this will actually reverse the contents of
the scalar.

Fixes evalEmpire/perl5i#182
  • Loading branch information...
1 parent dbfec8a commit 3607bc5d671bfa27a7510b9319f639534b858cec @doherty doherty committed with daxim Apr 23, 2011
Showing with 9 additions and 1 deletion.
  1. +3 −1 lib/autobox/Core.pm
  2. +6 −0 t/reverse.t
View
@@ -776,7 +776,9 @@ sub lcfirst ($) { CORE::lcfirst($_[0]); }
sub length ($) { CORE::length($_[0]); }
sub ord ($) { CORE::ord($_[0]); }
sub pack ($;@) { CORE::pack(shift, @_); }
-sub reverse ($) { CORE::reverse($_[0]); }
+sub reverse ($) {
+ wantarray ? return my $r = CORE::reverse($_[0]) : CORE::reverse($_[0]);
@schwern

schwern Sep 25, 2011

Collaborator

Better written as return scalar CORE::reverse($_[0]), no?

@doherty

doherty Sep 25, 2011

Contributor

Yes! I'm not sure why I did this... 14b9334

@scrottie

scrottie via email Sep 25, 2011

Owner
@doherty

doherty Sep 26, 2011

Contributor

/me looks at @schwern

@schwern

schwern Sep 26, 2011

Collaborator

I don't particularly need anything, but it wouldn't hurt to ship a release with a fixed reverse() and a proper change log.

I could cut a release today. @scrottie?

@scrottie

scrottie via email Sep 26, 2011

Owner
@schwern

schwern Sep 26, 2011

Collaborator

Done.

+}
sub rindex ($@) {
return CORE::rindex($_[0], $_[1]) if @_ == 2;
View
@@ -4,6 +4,12 @@ use warnings;
use autobox::Core;
+# https://github.com/schwern/perl5i/issues/182
+my $scalar = 'foo';
+my($reverse) = $scalar->reverse; # list context
+is $reverse, 'oof', 'reverse in list context reverses the scalar';
+is scalar $scalar->reverse, 'oof', 'reverse in scalar context reverses the scalar';
+
is "Hello"->reverse, "olleH";
my @list = qw(foo bar baz);

0 comments on commit 3607bc5

Please sign in to comment.