Subject: | RequireInterpolationOfMetachars allow "use vars" (patch) |
Date: | Thu, 25 Jun 2009 09:30:22 +1000 |
To: | bug-Perl-Critic [...] rt.cpan.org |
From: | Kevin Ryde <user42 [...] zip.com.au> |
RequireInterpolationOfMetachars could helpfully allow
use vars '$FOO';
I suppose most people write qw(), but quotes is quite nice for a single
variable. Grepping finds a few cpan modules with it, and perlfaq7 has
an example. The couple of lines below work for me.
Index: lib/Perl/Critic/Policy/ValuesAndExpressions/RequireInterpolationOfMetachars.pm
===================================================================
--- lib/Perl/Critic/Policy/ValuesAndExpressions/RequireInterpolationOfMetachars.pm (revision 3347)
+++ lib/Perl/Critic/Policy/ValuesAndExpressions/RequireInterpolationOfMetachars.pm (working copy)
@@ -64,6 +64,7 @@
# The string() method strips off the quotes
my $string = $elem->string();
return if _looks_like_use_overload( $elem );
+ return if _looks_like_use_vars_argument($elem);
return if not _needs_interpolation($string);
return if _looks_like_email_address($string);
@@ -126,6 +127,31 @@
return $stmt->type() eq q<use> && $stmt->module() eq q<overload>;
}
+#-----------------------------------------------------------------------------
+
+sub _looks_like_use_vars_argument {
+ my ( $elem ) = @_;
+
+ # "use vars '$x'" has the Include as the immediate parent of '$x'.
+ #
+ # "use vars ('$x')" inserts extra parent PPI::Statement::Expression and
+ # PPI::Structure::List elements.
+ #
+ # "use vars (('$x'))" has parent PPI::Statement too. Extra parens like
+ # this is a bit bizarre, but it runs ok, so support it.
+ #
+ my $parent = $elem;
+ do {
+ $parent = $parent->parent || return;
+ } while ($parent->isa('PPI::Structure::List')
+ || ($parent->isa('PPI::Statement')
+ && ! $parent->isa('PPI::Statement::Include')));
+
+ return ($parent->isa('PPI::Statement::Include')
+ && $parent->type eq 'use'
+ && $parent->module eq 'vars');
+}
+
1;
__END__
@@ -156,7 +182,33 @@
educated guess by looking for metacharacters and sigils which usually
indicate that the string should be interpolated.
+=head2 Allowed Strings
+=over 4
+
+=item *
+
+Email addresses,
+
+ send_email_to ('foo@bar.com', ...); # ok
+
+=item *
+
+C<${}> and C<@{}> in a C<use overload>,
+
+ use overload '${}' => \&deref, # ok
+ '@{}' => \&arrayize; # ok
+
+=item *
+
+Variable names to C<use vars>. You might prefer C<qw()> for multiple
+variables though.
+
+ use vars '$x'; # ok
+ use vars ('$y','$z'); # ok
+
+=back
+
=head1 CONFIGURATION
The C<rcs_keywords> option allows you to stop this policy from complaining
Index: t/ValuesAndExpressions/RequireInterpolationOfMetachars.run
===================================================================
--- t/ValuesAndExpressions/RequireInterpolationOfMetachars.run (revision 3347)
+++ t/ValuesAndExpressions/RequireInterpolationOfMetachars.run (working copy)
@@ -89,6 +89,29 @@
#-----------------------------------------------------------------------------
+## name use vars arguments
+## failures 0
+## cut
+
+use vars '$FOO';
+use vars '$FOO', '@BAR';
+use vars ('$FOO');
+use vars ('$FOO', '@BAR');
+use vars (('$FOO'));
+use vars (('$FOO', '@BAR'));
+use vars ((('$FOO')));
+use vars ((('$FOO', '@BAR')));
+
+#-----------------------------------------------------------------------------
+
+## name Not use vars
+## failures 1
+## cut
+
+use SomeModuleOtherThanVars '$FOO';
+
+#-----------------------------------------------------------------------------
+
## name Do complain about RCS variables, if not turned on.
## failures 7
## cut