NAME
Perl::Critic::Policy::ValuesAndExpressions::ProhibitUnknownBackslash - don't use undefined backslash forms
DESCRIPTION
This policy is part of the Perl::Critic::Pulp addon. It checks for unknown backslash escapes like
print "\*.c"; # bad
This is harmless, assuming the intention is a literal "*" (which it gives), but unnecessary, and on that basis this policy is under the cosmetic theme (see "POLICY THEMES" in Perl::Critic). Sometimes it can be a misunderstanding or a typo though, for instance a backslashed newline is a newline, but perhaps you thought it meant a continuation.
print "this\ # bad
is a newline";
Perl already warns about unknown escaped alphanumerics like \v under perl -w or use warnings (see "Unrecognized escape \\%c passed through" in perldiag).
print "\v"; # bad, and provokes Perl warning
This policy extends to report on any unknown escape, with options below to vary the strictness and to check single-quote strings too if desired.
Control Characters \c
Control characters \cX are checked and only the conventional A-Z a-z @ [ \ ] ^ _ ? are considered known.
print "\c*"; # bad
Perl accepts any \c and does an upcase and xor 0x40, so \c* is the letter j, on an ASCII system at least. But that's quite obscure and likely to be a typo or error.
For reference, \c\ is the ASCII FS "file separator" and the second backslash is not an escape, except for a closing quote character, which it does escape (basically because Perl scans for a closing quote before considering interpolations). Thus,
print " \c\ "; # ok, control-\ FS
print " \c\" "; # bad, control-" is unknown
print qq[ \c\] ]; # ok, control-] GS
Wide Chars
\N{} named unicode and \777 octal escapes above 255 are new in Perl 5.6. They're considered known if the document has a use 5.006 or higher, or if there's no use version at all.
print "\777"; # ok
use 5.006;
print "\N{APOSTROPHE}"; # ok
use 5.005;
print "\N{COLON}"; # bad
The absence of a use is treated as 5.6 because that's most likely, especially if you have those escapes intentionally. But perhaps this will change, or be configurable.
In the violation messages a non-ascii or non-graphical escaped char is shown as hex like \{0x263A}, to ensure the message is printable and unambiguous.
Other Notes
Interpolated $foo or @{expr} variables and expressions are parsed like Perl does, so backslashes for refs there are ok, in particular tricks like ${\scalar ...} are fine (see "How do I expand function calls in a string?" in perlfaq4).
print "this ${\(some()+thing())}"; # ok
As always, if you're not interested in any of this then you can disable ProhibitUnknownBackslash from your .perlcriticrc in the usual way,
[-ValuesAndExpressions::ProhibitUnknownBackslash]
CONFIGURATION
double(string, default "all")heredoc(string, default "all")-
doubleapplies to double-quote strings"",qq{},qx{}, etc.heredocapplies to interpolated here-documents<<HEREetc. The possible values arenone don't report anything alnum report unknown alphanumerics, like Perl's warning quotemeta report anything C<quotemeta> doesn't escape all report all unknowns"alnum" does no more than compiling with
perl -w, but might be good for checking code you don't want to run."quotemeta" reports escapes not produced by
quotemeta(). For examplequotemetaescapes a*, so\*is not reported, but it doesn't escape an underscore_, so\_is reported. The effect is to prohibit a few more escapes than "alnum". One use is to check code generated by other code where you've usedquotemetato produce double-quoted strings and thus may have escaping which is unnecessary but works fine. single(string, default "none")-
singleapplies to single-quote strings'',q{},qx'', etc. The possible values are as above, though only "all" or "none" make much sense.none don't report anything all report all unknownsThe default is "none" because literal backslashes in single-quotes are usually both what you want and quite convenient. Setting "all" effectively means you must write backslashes as
\\.print 'c:\my\msdos\filename'; # bad under "single=all" print 'c:\\my\\msdos\\filename'; # okDoubled backslashing like this is correct, and can emphasise that you really did want a backslash, but it's tedious and not easy on the eye and so is left only as an option.
For reference, single-quote here-documents
<<'HERE'don't have any backslash escapes and so are not considered by this policy.qx{}command backticks are double-quote but asqx''is single-quote and in each case treated under the corresponding single/double option.
SEE ALSO
Perl::Critic::Pulp, Perl::Critic, "Quote and Quote-like Operators" in perlop
HOME PAGE
http://user42.tuxfamily.org/perl-critic-pulp/index.html
COPYRIGHT
Copyright 2009, 2010 Kevin Ryde
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Perl-Critic-Pulp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses>.