1PPIx::Regexp::Token::MoUdsiefrieCro(n3t)ributed Perl DocPuPmIexn:t:aRteigoenxp::Token::Modifier(3)
2
3
4
6 PPIx::Regexp::Token::Modifier - Represent modifiers.
7
9 use PPIx::Regexp::Dumper;
10 PPIx::Regexp::Dumper->new( 'qr{foo}smx' )
11 ->print();
12
13 The trailing "smx" will be represented by this class.
14
15 This class also represents the whole of things like "(?ismx)". But the
16 modifiers in something like "(?i:foo)" are represented by a
17 PPIx::Regexp::Token::GroupType::Modifier.
18
20 "PPIx::Regexp::Token::Modifier" is a PPIx::Regexp::Token.
21
22 "PPIx::Regexp::Token::Modifier" is the parent of
23 PPIx::Regexp::Token::GroupType::Modifier.
24
26 This class represents modifier characters at the end of the regular
27 expression. For example, in "qr{foo}smx" this class would represent
28 the terminal "smx".
29
30 The "a", "aa", "d", "l", and "u" modifiers
31 The "a", "aa", "d", "l", and "u" modifiers, introduced starting in Perl
32 5.13.6, are used to force either Unicode pattern semantics ("u"),
33 locale semantics ("l") default semantics ("d" the traditional Perl
34 semantics, which can also mean 'dual' since it means Unicode if the
35 string's UTF-8 bit is on, and locale if the UTF-8 bit is off), or
36 restricted default semantics ("a"). These are mutually exclusive, and
37 only one can be asserted at a time. Asserting any of these overrides
38 the inherited value of any of the others. The asserted() method reports
39 as asserted the last one it sees, or none of them if it has seen none.
40
41 For example, given "PPIx::Regexp::Token::Modifier" $elem representing
42 the invalid regular expression fragment "(?dul)", "$elem->asserted( 'l'
43 )" would return true, but "$elem->asserted( 'u' )" would return false.
44 Note that "$elem->negated( 'u' )" would also return false, since "u" is
45 not explicitly negated.
46
47 If $elem represented regular expression fragment "(?i)",
48 "$elem->asserted( 'd' )" would return false, since even though "d"
49 represents the default behavior it is not explicitly asserted.
50
51 The caret ("^") modifier
52 Calling "^" a modifier is a bit of a misnomer. The "(?^...)"
53 construction was introduced in Perl 5.13.6, to prevent the inheritance
54 of modifiers. The documentation calls the caret a shorthand equivalent
55 for "d-imsx", and that it the way this class handles it.
56
57 For example, given "PPIx::Regexp::Token::Modifier" $elem representing
58 regular expression fragment "(?^i)", "$elem->asserts( 'd' )" would
59 return true, since in the absence of an explicit "l" or "u" this class
60 considers the "^" to explicitly assert "d".
61
62 The caret handling is complicated by the fact that the 'n' modifier was
63 introduced in 5.21.8, at which point the caret became equivalent to
64 "d-imnsx". I did not feel I could unconditionally add the "-n" to the
65 expansion of the caret, because that would produce confusing output
66 from methods like explain(). Nor could I make it conditional on the
67 minimum perl version, because that information is not available early
68 enough in the parse. What I did was to expand the caret into "d-imnsx"
69 if and only if 'n' was in effect at some point in the scope in which
70 the modifier was parsed.
71
72 Continuing the above example, "$elem->asserts( 'n' )" and
73 "$elem->modifier_asserted( 'n' )" would both return false, but
74 "$elem->negates( 'n' )" would return true if and only if the "/m"
75 modifier has been asserted somewhere before and in-scope from this
76 token. The modifier_asserted( 'n' ) method is inherited from
77 PPIx::Regexp::Element.
78
80 This class provides the following public methods. Methods not
81 documented here are private, and unsupported in the sense that the
82 author reserves the right to change or remove them without notice.
83
84 asserts
85 $token->asserts( 'i' ) and print "token asserts i";
86 foreach ( $token->asserts() ) { print "token asserts $_\n" }
87
88 This method returns true if the token explicitly asserts the given
89 modifier. The example would return true for the modifier in "(?i:foo)",
90 but false for "(?-i:foo)".
91
92 Starting with version 0.036_01, if the argument is a single-character
93 modifier followed by an asterisk (intended as a wild card character),
94 the return is the number of times that modifier appears. In this case
95 an exception will be thrown if you specify a multi-character modifier
96 (e.g. 'ee*').
97
98 If called without an argument, or with an undef argument, all modifiers
99 explicitly asserted by this token are returned.
100
101 match_semantics
102 my $sem = $token->match_semantics();
103 defined $sem or $sem = 'undefined';
104 print "This token has $sem match semantics\n";
105
106 This method returns the match semantics asserted by the token, as one
107 of the strings 'a', 'aa', 'd', 'l', or 'u'. If no explicit match
108 semantics are asserted, this method returns "undef".
109
110 modifiers
111 my %mods = $token->modifiers();
112
113 Returns all modifiers asserted or negated by this token, and the values
114 set (true for asserted, false for negated). If called in scalar
115 context, returns a reference to a hash containing the values.
116
117 negates
118 $token->negates( 'i' ) and print "token negates i\n";
119 foreach ( $token->negates() ) { print "token negates $_\n" }
120
121 This method returns true if the token explicitly negates the given
122 modifier. The example would return true for the modifier in
123 "(?-i:foo)", but false for "(?i:foo)".
124
125 If called without an argument, or with an undef argument, all modifiers
126 explicitly negated by this token are returned.
127
129 Support is by the author. Please file bug reports at
130 <https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-Regexp>,
131 <https://github.com/trwyant/perl-PPIx-Regexp/issues>, or in electronic
132 mail to the author.
133
135 Thomas R. Wyant, III wyant at cpan dot org
136
138 Copyright (C) 2009-2023 by Thomas R. Wyant, III
139
140 This program is free software; you can redistribute it and/or modify it
141 under the same terms as Perl 5.10.0. For more details, see the full
142 text of the licenses in the directory LICENSES.
143
144 This program is distributed in the hope that it will be useful, but
145 without any warranty; without even the implied warranty of
146 merchantability or fitness for a particular purpose.
147
148
149
150perl v5.36.0 2023-01-30 PPIx::Regexp::Token::Modifier(3)