1Perl::Critic::Utils::PPUIsReergeCxopn(t3r)ibuted Perl DoPceurmle:n:tCartiitoinc::Utils::PPIRegexp(3)
2
3
4
6 Perl::Critic::Utils::PPIRegexp - Utility functions for dealing with PPI
7 regexp tokens.
8
10 use Perl::Critic::Utils::PPIRegexp qw(:all);
11 use PPI::Document;
12 my $doc = PPI::Document->new(\'m/foo/');
13 my $elem = $doc->find('PPI::Token::Regexp::Match')->[0];
14 print get_match_string($elem); # yields 'foo'
15
17 As of PPI v1.1xx, the PPI regexp token classes
18 (PPI::Token::Regexp::Match, PPI::Token::Regexp::Substitute and
19 PPI::Token::QuoteLike::Regexp) has a very weak interface, so it is
20 necessary to dig into internals to learn anything useful. This package
21 contains subroutines to encapsulate that excess intimacy. If future
22 versions of PPI gain better accessors, this package will start using
23 those.
24
26 This is considered to be a public module. Any changes to its interface
27 will go through a deprecation cycle.
28
30 "parse_regexp( $token )"
31 Parse the regexp token with Regexp::Parser. If that module is not
32 available or if there is a parse error, returns undef. If a parse
33 success, returns a Regexp::Parser instance that can be used to walk
34 the regexp object model.
35
36 CAVEAT: This method pays special attention to the "x" modifier to
37 the regexp. If present, we wrap the regexp string in "(?x:...)" to
38 ensure a proper parse. This does change the object model though.
39
40 Someday if PPI gets native Regexp support, this method may become
41 deprecated.
42
43 "ppiify( $regexp )"
44 Given a Regexp::Parser instance (perhaps as returned from
45 "parse_regexp") convert it to a tree of PPI::Node instances. This
46 is useful because PPI has a more familiar and powerful programming
47 model than the Regexp::Parser object tree.
48
49 Someday if PPI gets native Regexp support, this method may become a
50 no-op.
51
52 "get_match_string( $token )"
53 Returns the match portion of the regexp or undef if the specified
54 token is not a regexp. Examples:
55
56 m/foo/; # yields 'foo'
57 s/foo/bar/; # yields 'foo'
58 / \A a \z /xms; # yields ' \\A a \\z '
59 qr{baz}; # yields 'baz'
60
61 "get_substitute_string( $token )"
62 Returns the substitution portion of a search-and-replace regexp or
63 undef if the specified token is not a valid regexp. Examples:
64
65 m/foo/; # yields undef
66 s/foo/bar/; # yields 'bar'
67
68 "get_modifiers( $token )"
69 Returns a hash containing booleans for the modifiers of the regexp,
70 or undef if the token is not a regexp.
71
72 /foo/xms; # yields (m => 1, s => 1, x => 1)
73 s/foo//; # yields ()
74 qr/foo/i; # yields (i => 1)
75
76 "get_delimiters( $token )"
77 Returns one (or two for a substitution regexp) two-character
78 strings indicating the delimiters of the regexp, or an empty list
79 if the token is not a regular expression token. For example:
80
81 m/foo/; # yields ('//')
82 m#foo#; # yields ('##')
83 m<foo>; # yields ('<>')
84 s/foo/bar/; # yields ('//', '//')
85 s{foo}{bar}; # yields ('{}', '{}')
86 s{foo}/bar/; # yields ('{}', '//') valid, but yuck!
87 qr/foo/; # yields ('//')
88
89 "regexp_interpolates( $token )"
90 Returns true if the given regexp interpolates, false if it does
91 not, or undef if the status can not be determined. The determining
92 factor is whether or not the first delimiting character (as
93 returned by "get_delimiters") is a single quote. For example:
94
95 m/foo/; # yields true
96 qr{foo}; # yields true
97 m'foo'; # yields false
98
100 Chris Dolan <cdolan@cpan.org>
101
103 Copyright (c) 2007-2013 Chris Dolan. Many rights reserved.
104
105 This program is free software; you can redistribute it and/or modify it
106 under the same terms as Perl itself. The full text of this license can
107 be found in the LICENSE file included with this module.
108
109
110
111perl v5.30.1 2020-01-30 Perl::Critic::Utils::PPIRegexp(3)