Login | Register
My pages Projects Community openCollabNet

Discussions > users > Parse "STATEMENT if (EXPR)" vs "if (EXPR) BLOCK "

perlcritic
Discussion topic

Back to topic list

Parse "STATEMENT if (EXPR)" vs "if (EXPR) BLOCK "

Author aff
Full name Andreas Faafeng
Date 2011-12-16 07:46:27 PST
Message I have managed to create a policy that works correctly with the
following run file:

## name basic passes
## failures 0
## cut
$log->debug( 'hello' ) if $log->is_debug();

#-------------------​--------------------​--------------------​------------------

## name standard failures
## failures 1
## cut

$log->debug( 'hello' );

#-------------------​--------------------​--------------------​------------------

The relevant sections of the policy include:

sub applies_to { return qw{ PPI::Statement }; }
sub violates {
  my ($self, $elem, undef) = @_;

  my @children = $elem->schildren();

  # not relevant unless debug is found
  return if ! grep { m/^debug$/ } @children;

  # pass if is_debug is found
  return if grep { m/^is_debug$/ } @children;

  return $self->violation($DESC, $EXPL, $elem);
}

#-------------------​--------------------​--------------------​------------------

The PPI parse tree for 'if ($log->is_debug()) { $log->debug( 'hello' )
; }' is quite different from '$log->debug( 'hello' ) if $log->is_debug();'

Is there a way to create a single 'violates' method to cover both
expressions?

--
Andreas

« Previous message in topic | 1 of 4 | Next message in topic »

Messages

Show all messages in topic

Parse "STATEMENT if (EXPR)" vs "if (EXPR) BLOCK " aff Andreas Faafeng 2011-12-16 07:46:27 PST
     Re: Parse "STATEMENT if (EXPR)" vs "if (EXPR) BLOCK " thaljef Jeffrey Ryan Thalhammer 2011-12-16 11:47:40 PST
     Re: Parse "STATEMENT if (EXPR)" vs "if (EXPR) BLOCK " thaljef Jeffrey Ryan Thalhammer 2011-12-21 21:05:15 PST
     Re: Parse "STATEMENT if (EXPR)" vs "if (EXPR) BLOCK " aff Andreas Faafeng 2011-12-22 05:34:21 PST
Messages per page: