From: Vincent Pit Date: Mon, 18 Aug 2008 17:06:34 +0000 (+0200) Subject: Updated doc X-Git-Tag: v1.00~2 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=a1d84fdc64b3007b8dd1560c5b6b123554a06ea5;p=perl%2Fmodules%2FRegexp-Wildcards.git Updated doc --- diff --git a/lib/Regexp/Wildcards.pm b/lib/Regexp/Wildcards.pm index 6e8913d..72fa0c3 100644 --- a/lib/Regexp/Wildcards.pm +++ b/lib/Regexp/Wildcards.pm @@ -28,7 +28,7 @@ our $VERSION = '0.08'; $re = $rw->convert('a{b?,c}*'); # Do it Unix shell style. $re = $rw->convert('a?,b*', 'win32'); # Do it Windows shell style. $re = $rw->convert('*{x,y}?', 'jokers'); # Process the jokers and escape the rest. - $re = $rw->convert('%a_c%', 'sql'); # Turn SQL wildcards into regexps. + $re = $rw->convert('%a_c%', 'sql'); # Turn SQL wildcards into regexps. $rw = Regexp::Wildcards->new( do => [ qw/jokers brackets/ ], # Do jokers and brackets. @@ -44,7 +44,7 @@ our $VERSION = '0.08'; In many situations, users may want to specify patterns to match but don't need the full power of regexps. Wildcards make one of those sets of simplified rules. This module converts wildcard expressions to Perl regular expressions, so that you can use them for matching. -It handles the C<*> and C shell jokers, as well as Unix bracketed alternatives C<{,}>, but also C<%> and C<_> SQL wildcards. It can also keep original C<(...)> groups. Backspace (C<\>) is used as an escape character. +It handles the C<*> and C jokers, as well as Unix bracketed alternatives C<{,}>, but also C<%> and C<_> SQL wildcards. It can also keep original C<(...)> groups. Backspace (C<\>) is used as an escape character. Typesets that mimic the behaviour of Windows and Unix shells are also provided. @@ -69,7 +69,7 @@ $types{$_} = $types{win32} for qw/dos os2 MSWin32 cygwin/; my %escapes = ( jokers => '?*', - sql => '%_', + sql => '_%', commas => ',', brackets => '{},', groups => '()', @@ -188,126 +188,114 @@ sub new { $self->capture($args{capture}); } -=head2 C<< new [ do => $what | type => $type ], capture => $captures >> +=head2 C<< new [ do => $what E type => $type ], capture => $captures >> Constructs a new L object. C lists all features that should be enabled when converting wildcards to regexps. Refer to L for details on what can be passed in C<$what>. -The C specifies a predefined set of C features to use. +The C specifies a predefined set of C features to use. See L for details on which types are valid. The C option overrides C. +C lists which atoms should be capturing. Refer to L for more details. -C<$type> can be any of C<'jokers'>, C<'sql'>, C<'commas'>, C<'brackets'>, C<'win32'> or C<'unix'>. An unknown value defaults to C<'unix'>, except for C<'dos'>, C<'os2'>, C<'MSWin32'> and C<'cygwin'> that default to C<'win32'>. With this set of options, you can pass C<$^O> as the C<$type> so that you get the corresponding shell behaviour. +=head2 C<< do [ $what E set => $c1, add => $c2, rem => $c3 ] >> -=over 4 +Specifies the list of metacharacters to convert. +They are classified into five classes : -=item C<> +=over 4 -For the C<$capture> syntax, refer to the L method. +=item C<'jokers'> converts C to C<.> and C<*> to C<.*> ; -=head3 C + 'a**\\*b??\\?c' ==> 'a.*\\*b..\\?c' -=head3 C +=item C<'sql'> converts C<_> to C<.> and C<%> to C<.*> ; -=over 4 + 'a%%\\%b__\\_c' ==> 'a.*\\%b..\\_c' -=head2 C<$CaptureSingle> +=item C<'commas'> converts all C<,> to C<|> and puts the complete resulting regular expression inside C<(?: ... )> ; -When this variable is true, each occurence of unescaped I<"exactly one"> wildcards (i.e. C jokers or C<_> for SQL wildcards) are made capturing in the resulting regexp (they are be replaced by C<(.)>). Otherwise, they are just replaced by C<.>. Default is the latter. + 'a,b{c,d},e' ==> '(?:a|b\\{c|d\\}|e)' - For jokers : - 'a???b\\??' is translated to 'a(.)(.)(.)b\\?(.)' if $CaptureSingle is true - 'a...b\\?.' otherwise (default) +=item C<'brackets'> converts all matching C<{ ... , ... }> brackets to C<(?: ... | ... )> alternations. If some brackets are unbalanced, it tries to substitute as many of them as possible, and then escape the remaining unmatched C<{> and C<}>. Commas outside of any bracket-delimited block are also escaped ; - For SQL wildcards : - 'a___b\\__' is translated to 'a(.)(.)(.)b\\_(.)' if $CaptureSingle is true - 'a...b\\_.' otherwise (default) + 'a,b{c,d},e' ==> 'a\\,b(?:c|d)\\,e' + '{a\\{b,c}d,e}' ==> '(?:a\\{b|c)d\\,e\\}' + '{a{b,c\\}d,e}' ==> '\\{a\\{b\\,c\\}d\\,e\\}' +=item C<'groups'> keeps the parenthesis C<( ... )> of the original string without escaping them. Currently, no check is done to ensure that the parenthesis are matching. -=item C + 'a(b(c))d\\(\\)' ==> (no change) -By default this variable is false, and successions of unescaped I<"any"> wildcards (i.e. C<*> jokers or C<%> for SQL wildcards) are replaced by B single C<.*>. When it evalutes to true, those sequences of I<"any"> wildcards are made into B capture, which is greedy (C<(.*)>) for C<$CaptureAny E 0> and otherwise non-greedy (C<(.*?)>). +=back - For jokers : - 'a***b\\**' is translated to 'a.*b\\*.*' if $CaptureAny is false (default) - 'a(.*)b\\*(.*)' if $CaptureAny > 0 - 'a(.*?)b\\*(.*?)' otherwise +Each C<$c> can be any of : - For SQL wildcards : - 'a%%%b\\%%' is translated to 'a.*b\\%.*' if $CaptureAny is false (default) - 'a(.*)b\\%(.*)' if $CaptureAny > 0 - 'a(.*?)b\\%(.*?)' otherwise +=over 4 -=item C +=item A hash reference, with wanted metacharacter group names (described above) as keys and booleans as values ; -If this variable is set to true, valid brackets constructs are made into C<( | )> captures, and otherwise they are replaced by non-capturing alternations (C<(?: | >)), which is the default. +=item An array reference containing the list of wanted metacharacter classes ; - 'a{b\\},\\{c}' is translated to 'a(b\\}|\\{c)' if $CaptureBrackets is true - 'a(?:b\\}|\\{c)' otherwise (default) +=item A plain scalar, when only one group is required. =back -=head2 C +When C is present, the classes given as its value replace the current object options. Then the C classes are added, and the C classes removed. -This function takes as its only argument the wildcard string to process, and returns the corresponding regular expression where the jokers C (I<"exactly one">) and C<*> (I<"any">) have been translated into their regexp equivalents (see L for more details). All other unprotected regexp metacharacters are escaped. +Passing a sole scalar C<$what> is equivalent as passing C<< set => $what >>. +No argument means C<< set => [ ] >>. - # Everything is escaped. - print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}'; - -=cut - -=head2 C - -Similar to the precedent, but for the SQL wildcards C<_> (I<"exactly one">) and C<%> (I<"any">). All other unprotected regexp metacharacters are escaped. - -=cut - -=head2 C + $rw->do(set => 'jokers'); # Only translate jokers. + $rw->do('jokers'); # Same. + $rw->do(add => [ qw/sql commas/ ]); # Translate also SQL and commas. + $rw->do(rem => 'jokers'); # Specifying both 'sql' and 'jokers' is useless. + $rw->do(); # Translate nothing. -This function conforms to standard Unix shell wildcard rules. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, turns C and C<*> jokers into their regexp equivalents (see L), and changes bracketed blocks into (possibly capturing) alternations as described in L. If brackets are unbalanced, it tries to substitute as many of them as possible, and then escape the remaining C<{> and C<}>. Commas outside of any bracket-delimited block are also escaped. +=head2 C - # This is a valid bracket expression, and is completely translated. - print 'ok' if wc2re_unix('{a{b,c}d,e}') eq '(?:a(?:b|c)d|e)'; +Notifies to convert the metacharacters that corresponds to the predefined type C<$type>. C<$type> can be any of C<'jokers'>, C<'sql'>, C<'commas'>, C<'brackets'>, C<'win32'> or C<'unix'>. An unknown or undefined value defaults to C<'unix'>, except for C<'dos'>, C<'os2'>, C<'MSWin32'> and C<'cygwin'> that default to C<'win32'>. This means that you can pass C<$^O> as the C<$type> and get the corresponding shell behaviour. Returns the object. -The function handles unbalanced bracket expressions, by escaping everything it can't recognize. For example : + $rw->type('win32'); # Set type to win32. + $rw->type(); # Set type to unix. - # The first comma is replaced, and the remaining brackets and comma are escaped. - print 'ok' if wc2re_unix('{a\\{b,c}d,e}') eq '(?:a\\{b|c)d\\,e\\}'; +=head2 C<< capture [ $captures E set => $c1, add => $c2, rem => $c3 ] >> - # All the brackets and commas are escaped. - print 'ok' if wc2re_unix('{a{b,c\\}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}'; +Specifies the list of atoms to capture. +This method works like L, except that the classes are different : -This one works just like the one before, but for Windows wildcards. Bracketed blocks are no longer handled (which means that brackets are escaped), but you can provide a comma-separated list of items. +=over 4 - # All the brackets are escaped, and commas are seen as list delimiters. - print 'ok' if wc2re_win32('{a{b,c}d,e}') eq '(?:\\{a\\{b|c\\}d|e\\})'; +=item C<'single'> will capture all unescaped I<"exactly one"> metacharacters, i.e. C for wildcards or C<_> for SQL ; -=cut + 'a???b\\??' ==> 'a(.)(.)(.)b\\?(.)' + 'a___b\\__' ==> 'a(.)(.)(.)b\\_(.)' -=head2 C +=item C<'any'> will capture all unescaped I<"any"> metacharacters, i.e. C<*> for wildcards or C<%> for SQL ; -A generic function that wraps around all the different rules. The first argument is the wildcard expression, and the second one is the type of rules to apply which can be : + 'a***b\\**' ==> 'a(.*)b\\*(.*)' + 'a%%%b\\%%' ==> 'a(.*)b\\%(.*)' -=over 4 +=item C<'greedy'>, when used in conjunction with C<'any'>, will make the C<'any'> captures greedy (by default they are not) ; -=item C<'unix'>, C<'win32'>, C<'jokers'>, C<'sql'> + 'a***b\\**' ==> 'a(.*?)b\\*(.*?)' + 'a%%%b\\%%' ==> 'a(.*?)b\\%(.*?)' -For one of those raw rule names, C simply maps to C, C, C and C respectively. +=item C<'brackets'> will capture matching C<{ ... , ... }> alternations. -=item C<$^O> - -If you supply the Perl operating system name, the call is deferred to C for C< $^O> equal to C<'dos'>, C<'os2'>, C<'MSWin32'> or C<'cygwin'>, and to C in all the other cases. + 'a{b\\},\\{c}' ==> 'a(b\\}|\\{c)' =back -If the type is undefined or not supported, it defaults to C<'unix'>. + $rw->capture(set => 'single'); # Only capture "exactly one" metacharacters. + $rw->capture('single'); # Same. + $rw->capture(add => [ qw/any greedy/ ]); # Also greedily capture "any" metacharacters. + $rw->capture(rem => 'greedy'); # No more greed please. + $rw->capture(); # Capture nothing. - # Wraps to wc2re_jokers ($re eq 'a\\{b\\,c\\}.*'). - $re = wc2re 'a{b,c}*' => 'jokers'; +=head2 C - # Wraps to wc2re_win32 ($re eq '(?:a\\{b|c\\}.*)') - # or wc2re_unix ($re eq 'a(?:b|c).*') depending on $^O. - $re = wc2re 'a{b,c}*' => $^O; +Converts the wildcard expression C<$wc> into a regular expression according to the options stored into the L object, or to C<$type> if it's supplied. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, then replace C<'jokers'> or C<'sql'> and C<'commas'> or C<'brackets'> (depending on the L or L options), all of this by applying the C<'capture'> rules specified in the constructor or by L. =cut @@ -348,16 +336,6 @@ L, which is bundled with perl since version 5.7.3 This module does not implement the strange behaviours of Windows shell that result from the special handling of the three last characters (for the file extension). For example, Windows XP shell matches C<*a> like C<.*a>, C<*a?> like C<.*a.?>, C<*a??> like C<.*a.{0,2}> and so on. -=head1 SEE ALSO - -Some modules provide incomplete alternatives as helper functions : - -L has a method for that. Only jokers are translated, and escaping won't preserve them. - -L has a C function that compiles a matcher. It only handles C<*>. - -L has the C class method that handles jokers. - =head1 AUTHOR Vincent Pit, C<< >>, L. diff --git a/t/92-pod-coverage.t b/t/92-pod-coverage.t index f2a9f19..3037c13 100644 --- a/t/92-pod-coverage.t +++ b/t/92-pod-coverage.t @@ -16,6 +16,4 @@ my $min_pc = 0.18; eval "use Pod::Coverage $min_pc"; plan skip_all => "Pod::Coverage $min_pc required for testing POD coverage" if $@; -all_pod_coverage_ok( - { also_private => [ qr/^do_/, qr/^capture_/, qw/extract/ ] } -); +all_pod_coverage_ok();