Perl::Critic::Utils::PPIRegexp - Utility functions for dealing with PPI regexp tokens. |
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 (the PPI::Token::Regexp::Match manpage, the PPI::Token::Regexp::Substitute manpage and the PPI::Token::QuoteLike::Regexp manpage) 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 )
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 )
parse_regexp
)
convert it to a tree of the PPI::Node manpage 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 )
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 )
m/foo/; # yields undef s/foo/bar/; # yields 'bar'
get_modifiers( $token )
/foo/xms; # yields (m => 1, s => 1, x => 1) s/foo//; # yields () qr/foo/i; # yields (i => 1)
get_delimiters( $token )
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 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.
Perl::Critic::Utils::PPIRegexp - Utility functions for dealing with PPI regexp tokens. |