This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Call string overloading once in join($ov,...)
authorFather Chrysostomos <sprout@cpan.org>
Sun, 19 Oct 2014 21:14:05 +0000 (14:14 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Mon, 20 Oct 2014 19:38:06 +0000 (12:38 -0700)
doop.c
t/op/join.t

diff --git a/doop.c b/doop.c
index 007ff5e..62edb06 100644 (file)
--- a/doop.c
+++ b/doop.c
@@ -669,12 +669,10 @@ Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
     I32 items = sp - mark;
     STRLEN len;
     STRLEN delimlen;
+    const char * const delims = SvPV_const(delim, delimlen);
 
     PERL_ARGS_ASSERT_DO_JOIN;
 
-    (void) SvPV_const(delim, delimlen); /* stringify and get the delimlen */
-    /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
-
     mark++;
     len = (items > 0 ? (delimlen * (items - 1) ) : 0);
     SvUPGRADE(sv, SVt_PV);
@@ -708,10 +706,11 @@ Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
     }
 
     if (delimlen) {
+       const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
        for (; items > 0; items--,mark++) {
            STRLEN len;
            const char *s;
-           sv_catsv_nomg(sv,delim);
+           sv_catpvn_flags(sv,delims,delimlen,delimflag);
            s = SvPV_const(*mark,len);
            sv_catpvn_flags(sv,s,len,
                            DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
index f98b5db..4117d49 100644 (file)
@@ -6,7 +6,7 @@ BEGIN {
     require './test.pl';
 }
 
-plan tests => 26;
+plan tests => 28;
 
 @x = (1, 2, 3);
 is( join(':',@x), '1:2:3', 'join an array with character');
@@ -117,3 +117,10 @@ is( $f, 'baeak', 'join back to self, self is join character');
   is( $ju2, $u );
 }
 
+package o { use overload q|""| => sub { ${$_[0]}++ } }
+{
+  my $o = bless \(my $dummy = "a"), o::;
+  $_ = join $o, 1..10;
+  is $_, "1a2a3a4a5a6a7a8a9a10", 'join, $overloaded, LIST';
+  is $$o, "b", 'overloading was called once on overloaded separator';
+}