++ed by:

55 PAUSE users
36 non-PAUSE users.

Elliot Shank
and 1 contributors


Perl::Critic::Utils::PPIRegexp - Utility functions for dealing with PPI regexp tokens.


   use Perl::Critic::Utils::PPIRegexp qw(:all);
   use PPI::Document;
   my $doc = PPI::Document->new(\'m/foo/');
   my $elem = $doc->find('PPI::Token::Regexp::Match')->[0];
   print get_match_string($elem);  # yields 'foo'


As of PPI v1.1xx, the PPI regexp token classes (PPI::Token::Regexp::Match, PPI::Token::Regexp::Substitute and PPI::Token::QuoteLike::Regexp) has a very weak interface, so it is necessary to dig into internals to learn anything useful. This package contains subroutines to encapsulate that excess intimacy. If future versions of PPI gain better accessors, this package will start using those.


parse_regexp( $token )

Parse the regexp token with Regexp::Parser. If that module is not available or if there is a parse error, returns undef. If a parse success, returns a Regexp::Parser instance that can be used to walk the regexp object model.

CAVEAT: This method pays special attention to the x modifier to the regexp. If present, we wrap the regexp string in (?x:...) to ensure a proper parse. This does change the object model though.

Someday if PPI gets native Regexp support, this method may become deprecated.

ppiify( $regexp )

Given a Regexp::Parser instance (perhaps as returned from parse_regexp) convert it to a tree of PPI::Node instances. This is useful because PPI has a more familiar and powerful programming model than the Regexp::Parser object tree.

Someday if PPI gets native Regexp support, this method may become a no-op.

get_match_string( $token )

Returns the match portion of the regexp or undef if the specified token is not a regexp. Examples:

  m/foo/;         # yields 'foo'
  s/foo/bar/;     # yields 'foo'
  / \A a \z /xms; # yields ' \\A a \\z '
  qr{baz};        # yields 'baz'
get_substitute_string( $token )

Returns the substitution portion of a search-and-replace regexp or undef if the specified token is not a valid regexp. Examples:

  m/foo/;         # yields undef
  s/foo/bar/;     # yields 'bar'
get_modifiers( $token )

Returns a hash containing booleans for the modifiers of the regexp, or undef if the token is not a regexp.

  /foo/xms;  # yields (m => 1, s => 1, x => 1)
  s/foo//;   # yields ()
  qr/foo/i;  # yields (i => 1)
get_delimiters( $token )

Returns one (or two for a substitution regexp) two-character strings indicating the delimiters of the regexp, or an empty list if the token is not a regular expression token. For example:

   m/foo/;      # yields ('//')
   m#foo#;      # yields ('##')
   m<foo>;      # yields ('<>')
   s/foo/bar/;  # yields ('//', '//')
   s{foo}{bar}; # yields ('{}', '{}')
   s{foo}/bar/; # yields ('{}', '//')   valid, but yuck!
   qr/foo/;     # yields ('//')


Chris Dolan <cdolan@cpan.org>


Copyright (c) 2007-2008 Chris Dolan. Many rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.