Skip to content

Commit 00f271f

Browse files
committed
don't warn on 'undef $^W'
Ironically, this warns: $ perl -e'use warnings; undef $^W' Use of uninitialized value in undef operator at -e line 1. $ The magic-setting code was treating the new value of $^W as an integer. This commit makes it treat the value as a boolean. I suppose this commit could in theory break code if that code is doing something like: $^W = "0 but true"; In the past that would have disabled warnings, but will now enable them. But it seems unlikely that anyone would have written such code. The variable is documented in perlvar as having a value which is interpreted as a boolean. Note that this commit stops a test in t/op/reset.t from expecting a warning when resetting $^W. This test was added by issue GH #20763, and AFAIKT that ticket was concerned with 'reset $^W' not actually resetting the variable; the test for the warning was purely a side-effect of the fact that it happened to warn.
1 parent 067d8a6 commit 00f271f

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

mg.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,9 +3152,8 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
31523152
case '\027': /* ^W & $^WARNING_BITS */
31533153
if (*(mg->mg_ptr+1) == '\0') {
31543154
if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
3155-
i = SvIV(sv);
31563155
PL_dowarn = (PL_dowarn & ~G_WARN_ON)
3157-
| (i ? G_WARN_ON : G_WARN_OFF) ;
3156+
| (SvTRUE_NN(sv) ? G_WARN_ON : G_WARN_OFF) ;
31583157
}
31593158
}
31603159
else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {

t/lib/warnings/mg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,8 @@ EXPECT
169169
Use of uninitialized value $s in hash element at - line 13.
170170
Use of uninitialized value $_[0] in defined operator at - line 10.
171171
defined
172+
########
173+
# NAME undefined $^W shouldn't warn
174+
use warnings;
175+
undef $^W
176+
EXPECT

t/op/reset.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ SKIP:
197197
my $warn = '';
198198
local $SIG{__WARN__} = sub { $warn .= "@_\n" };
199199
reset "\cW";
200-
like($warn, qr/uninitialized/, "magic tries to SvIV() the new value");
200+
is($warn, "", 'no warnings resetting $^W');
201201
$warn = '';
202202
is($^W, 0, q"check $^W has been zeroed");
203203
is($warn, '', "should be no more warnings");

0 commit comments

Comments
 (0)