From: Gisle Aas Date: Fri, 18 May 2001 07:55:25 +0000 (-0700) Subject: Chomp should not always stringify X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9b33ce3b1592d0c13aec55cf7c63e0b9253b6b22;p=p5sagit%2Fp5-mst-13.2.git Chomp should not always stringify Message-ID: p4raw-id: //depot/perl@10161 --- diff --git a/doop.c b/doop.c index 755cbfd..9550b3e 100644 --- a/doop.c +++ b/doop.c @@ -989,6 +989,7 @@ Perl_do_chomp(pTHX_ register SV *sv) { register I32 count; STRLEN len; + STRLEN n_a; char *s; if (RsSNARF(PL_rs)) @@ -1020,8 +1021,6 @@ Perl_do_chomp(pTHX_ register SV *sv) else if (SvREADONLY(sv)) Perl_croak(aTHX_ PL_no_modify); s = SvPV(sv, len); - if (len && !SvPOKp(sv)) - s = SvPV_force(sv, len); if (s && len) { s += --len; if (RsPARA(PL_rs)) { @@ -1052,12 +1051,13 @@ Perl_do_chomp(pTHX_ register SV *sv) count += rslen; } } - *s = '\0'; + s = SvPV_force(sv, n_a); SvCUR_set(sv, len); + *SvEND(sv) = '\0'; SvNIOK_off(sv); + SvSETMAGIC(sv); } nope: - SvSETMAGIC(sv); return count; } diff --git a/t/op/chop.t b/t/op/chop.t index 1b55f11..e8b777e 100755 --- a/t/op/chop.t +++ b/t/op/chop.t @@ -1,6 +1,6 @@ #!./perl -print "1..37\n"; +print "1..41\n"; # optimized @@ -116,3 +116,13 @@ print chop(@stuff[0, 2]) eq 'f' ? "ok 36\n" : "not ok 36\n"; my %stuff = (1..4); print chop(@stuff{1, 3}) eq '4' ? "ok 37\n" : "not ok 37\n"; + +# chomp should not stringify references unless it decides to modify them +$_ = []; +$/ = "\n"; +print chomp() == 0 ? "ok 38\n" : "not ok 38\n"; +print ref($_) eq "ARRAY" ? "ok 39\n" : "not ok 39\n"; + +$/ = ")"; # the last char of something like "ARRAY(0x80ff6e4)" +print chomp() == 1 ? "ok 40\n" : "not ok 40\n"; +print !ref($_) ? "ok 41\n" : "not ok 41\n";