summaryrefslogtreecommitdiff
path: root/mg.c
diff options
context:
space:
mode:
authorYves Orton <yves.orton@booking.com>2014-02-03 22:20:13 +0800
committerYves Orton <yves.orton@booking.com>2014-02-03 22:44:30 +0800
commitb3a2acfa0c0e4f8e48e1f6eb4d6fd143f293d2c6 (patch)
tree0f5a95628cc10b3eddaa1d68d8dd1ae30f24bb01 /mg.c
parentee273784a82417ff7a4ec06716556fb7fb705427 (diff)
downloadperl-b3a2acfa0c0e4f8e48e1f6eb4d6fd143f293d2c6.tar.gz
deal with assignment to $/ better, deprecate edge cases, and forbid others
The actual behavior of $/ under various settings and how it is documented varies quite a bit. Clarify the documentation, and add various checks that are validated when setting $/. The gist of the problem was that the way that weirdo ref assignments were handled was mostly broken: * setting to a reference to an array, hash, or other higher level construct would behave similarly to setting it to a reference to a an integer, by numifying the ref and using it as an integer. This behavior was entirely undocumented. * setting to a reference to 0 or to -1 was *documented* in triggering "slurp" behavior, but actually did not. Instead it would set the separator to the stringified form of the ref, which would *appear* as slurp behavior due to the unlikelihood of a file actually containing a string which matched, however was less efficient, and if someone's luck were *terrible* might actually behave as a split. In the future we wish to support more sophisticated ways of setting the input record separator, possibly supporting things like: $/= [ "foo", "bar" ]; $/= qr/foo|bar/; Accordingly this patch *forbids* the use of a non scalar ref, and raises a fatal exception when one does so. Additionally it treats non-positive refs *exactly* the same as assigning undef, *including* ignoring the original value and setting $/ to undef. The means the implementation now matches the documentation. However since this might involve some crazy script changing in behavior (as one can't fetch back the original ref from $/) I have added a warning in category "deprecated" advising the user what has happened and recommending setting to "undef" explicitly. As far as I can tell this will only *break* code doing extremely dodgy things with $/. While putting together this patch I encountered numerous problems with porting tests. First off was porting/podcheck.t, which failed test without saying why or what to do, even under TEST_VERBOSE=1. Then when I did a regen to update the exceptions database and then used that information to try to fix the reported problems it seems that it does not work properly anyway. Specifically you aren't allowed to have a / in the interesting parts of a L<> reference. If you replace the / with an E<0x2f> then the link is valid POD, but podcheck.t then considers it a broken link. If you then replace the / in perdiag with E<0x2f> as well then porting/diag.t complains that you have an undocumented diagnostic! Accordingly I used the --regen option of podcheck.t to add exceptions to the exception database. I have no idea if the pod is correctly formatted or not.
Diffstat (limited to 'mg.c')
-rw-r--r--mg.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/mg.c b/mg.c
index a8032f50bf..6d04536a62 100644
--- a/mg.c
+++ b/mg.c
@@ -2749,8 +2749,20 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
}
break;
case '/':
- SvREFCNT_dec(PL_rs);
- PL_rs = newSVsv(sv);
+ if (!SvROK(sv) || ( SvTYPE(SvRV(sv)) < SVt_PVAV && SvIV(SvRV(sv)) > 0 ) ) {
+ SvREFCNT_dec(PL_rs);
+ PL_rs = newSVsv(sv);
+ } else if (SvTYPE(SvRV(sv)) >= SVt_PVAV) {
+ Perl_croak(aTHX_ "Setting $/ to a %s reference is forbidden", sv_reftype(SvRV(sv),0));
+ } else {
+ /* treat as undef */
+ Perl_ck_warner(aTHX_ packWARN(WARN_DEPRECATED),
+ "Setting $/ to a reference to %s as a form of slurp is deprecated, treating as undef",
+ SvIV(SvRV(sv)) < 0 ? "a negative integer" : "zero"
+ );
+ SvREFCNT_dec(PL_rs);
+ PL_rs= newSVsv(&PL_sv_undef);
+ }
break;
case '\\':
SvREFCNT_dec(PL_ors_sv);