Benchmark-Perl-Formance-Cargo

 view release on metacpan or  search on metacpan

share/PerlCritic/Critic/Policy/Variables/ProhibitPunctuationVars.pm  view on Meta::CPAN


    # Set up the regexp alternation for matching magic variables.
    # We can't process $config->{_allow} here because of a quirk in the
    # way Perl::Critic handles testing.
    #
    # The sort is needed so that, e.g., $^ doesn't mask out $^M
    my $magic_alternation =
            '(?:'
        .   (
            join
                q<|>,
                map          { quotemeta $_ }
                reverse sort { length $a <=> length $b }
                grep         { q<%> ne substr $_, 0, 1 }
                @MAGIC_VARIABLES
        )
        .   ')';

    return qr<
        (?: \A | [^\\] )       # beginning-of-string or any non-backslash
        (?: \\{2} )*           # zero or more double-backslashes
        ( $magic_alternation ) # any magic punctuation variable
    >xsm;
}

1;

__END__

#-----------------------------------------------------------------------------

=pod

=head1 NAME

Perl::Critic::Policy::Variables::ProhibitPunctuationVars - Write C<$EVAL_ERROR> instead of C<$@>.


=head1 AFFILIATION

This Policy is part of the core L<Perl::Critic|Perl::Critic>
distribution.


=head1 DESCRIPTION

Perl's vocabulary of punctuation variables such as C<$!>, C<$.>, and
C<$^> are perhaps the leading cause of its reputation as inscrutable
line noise.  The simple alternative is to use the L<English|English>
module to give them clear names.

  $| = undef;                      #not ok

  use English qw(-no_match_vars);
  local $OUTPUT_AUTOFLUSH = undef; #ok

=head1 CONFIGURATION

The scratch variables C<$_> and C<@_> are very common and are pretty
well understood, so they are exempt from this policy.  The same goes
for the less-frequently-used default filehandle C<_> used by stat().
All the regexp capture variables (C<$1>, C<$2>, ...) are exempt too.
C<$]> is exempt because there is no L<English|English> equivalent and
L<Module::CoreList|Module::CoreList> is based upon it.

You can add more exceptions to your configuration.  In your
perlcriticrc file, add a block like this:

  [Variables::ProhibitPunctuationVars]
  allow = $@ $!

The C<allow> property  should  be  a  whitespace-delimited  list  of
punctuation variables.

Other configuration options  control  the  parsing  of  interpolated
strings in the search for forbidden variables. They have  no  effect
on detecting punctuation variables outside of interpolated  strings.

  [Variables::ProhibitPunctuationVars]
  string_mode = thorough

The option C<string_mode>  controls  whether  and  how  interpolated
strings are searched for punctuation variables. Setting
C<string_mode = thorough>, the default,  checks  for  special  cases
that may look like punctuation variables  but  aren't,  for  example
C<$#foo>, an array index count; C<$$bar>, a scalar  dereference;  or
C<$::baz>, a global symbol.

Setting C<string_mode = disable> causes all interpolated strings  to
be ignored entirely.

Setting C<string_mode = simple> uses a simple regular expression  to
find matches. In this mode, the magic variables C<$$>, C<$'>,  C<$#>
and C<$:> are ignored within interpolated strings due  to  the  high
risk of false positives. Simple mode is  retained  from  an  earlier
draft of the interpolated- strings code. Its use is only recommended
as a workaround if bugs appear in thorough mode.

The  C<string_mode>  option  will  go  away  when  the  parsing   of
interpolated strings is implemented in PPI. See  L</CAVEATS>  below.


=head1 BUGS

Punctuation variables that confuse PPI's document parsing may not be
detected  correctly  or  at  all,  and  may  prevent  detection   of
subsequent ones. In particular, C<$"> is known to cause difficulties
in interpolated strings.


=head1 CAVEATS

ProhibitPunctuationVars  relies   exclusively   on   PPI   to   find
punctuation variables in code, but does all the parsing  itself  for
interpolated strings. When, at some  point,  this  functionality  is
transferred to PPI, ProhibitPunctuationVars  will  cease  doing  the
interpolating  and  the  C<string_mode>   option   will   go   away.


=head1 AUTHOR



( run in 0.558 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )