File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -5480,6 +5480,33 @@ character class, just escape the square brackets with the backslash: "\[="
5480
5480
and "=\]". The S<<-- HERE> shows whereabouts in the regular expression the
5481
5481
problem was discovered. See L<perlre>.
5482
5482
5483
+ =item Possible attempt to escape whitespace in qw() list
5484
+
5485
+ (W qw) qw() lists contain items separated by whitespace; contrary to
5486
+ what some might expect, backslash characters cannot be used to "protect"
5487
+ whitespace from being split, but are instead treated as literal data.
5488
+ (You may have used different delimiters than the parentheses shown here;
5489
+ braces are also frequently used.)
5490
+
5491
+ You probably wrote something like this:
5492
+
5493
+ @list = qw(
5494
+ a\ string
5495
+ another
5496
+ );
5497
+
5498
+ expecting to get a two elements list containing the strings C<'a string'>
5499
+ and C<'another'>. Instead the list will hold four elements: C<'a\'>
5500
+ (with a literal backslash), C<'string'> and C<'another'>.
5501
+
5502
+ If you really want whitespace in your strings, build your list the
5503
+ old-fashioned way, with quotes and commas:
5504
+
5505
+ @list = ( 'a string', 'another' );
5506
+
5507
+ Note that this warnings is I<only> emitted when the backslash is followed
5508
+ by actual whitespace (that C<qw> splits on).
5509
+
5483
5510
=item Possible attempt to put comments in qw() list
5484
5511
5485
5512
(W qw) qw() lists contain items separated by whitespace; as with literal
Original file line number Diff line number Diff line change @@ -49,6 +49,9 @@ toke.c AOK
49
49
Possible attempt to put comments in qw() list
50
50
@a = qw(a b # c) ;
51
51
52
+ Possible attempt to escape whitespace in qw() list
53
+ @a = qw( foo bar\ baz ) ;
54
+
52
55
%s (...) interpreted as function
53
56
print ("")
54
57
printf ("")
@@ -366,6 +369,12 @@ Possible attempt to separate words with commas at - line 3.
366
369
Possible attempt to put comments in qw() list at - line 3.
367
370
########
368
371
# toke.c
372
+ use warnings 'qw';
373
+ @a = qw( foo bar\ baz );
374
+ EXPECT
375
+ Possible attempt to escape whitespace in qw() list at - line 3.
376
+ ########
377
+ # toke.c
369
378
use warnings 'syntax' ;
370
379
print ("");
371
380
print ("") and $x = 1;
Original file line number Diff line number Diff line change @@ -5818,6 +5818,7 @@ yyl_qw(pTHX_ char *s, STRLEN len)
5818
5818
if (SvCUR (PL_lex_stuff )) {
5819
5819
int warned_comma = !ckWARN (WARN_QW );
5820
5820
int warned_comment = warned_comma ;
5821
+ int warned_escape = warned_comma ;
5821
5822
char * d = SvPV_force (PL_lex_stuff , len );
5822
5823
while (len ) {
5823
5824
for (; isSPACE (* d ) && len ; -- len , ++ d )
@@ -5837,6 +5838,11 @@ yyl_qw(pTHX_ char *s, STRLEN len)
5837
5838
"Possible attempt to put comments in qw() list" );
5838
5839
++ warned_comment ;
5839
5840
}
5841
+ else if (!warned_escape && * d == '\\' && len > 1 && isSPACE (* (d + 1 )) ) {
5842
+ warner (packWARN (WARN_QW ),
5843
+ "Possible attempt to escape whitespace in qw() list" );
5844
+ ++ warned_escape ;
5845
+ }
5840
5846
}
5841
5847
}
5842
5848
else {
You can’t perform that action at this time.
0 commit comments