home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / Regexp / Common.pm
Encoding:
Perl POD Document  |  2011-12-11  |  26.2 KB  |  882 lines

  1. package Regexp::Common;
  2.  
  3. use 5.00473;
  4. use strict;
  5.  
  6. BEGIN {
  7.     # This makes sure 'use warnings' doesn't bomb out on 5.005_*;
  8.     # warnings won't be enabled on those old versions though.
  9.     # Since all other files use this file, we can use 'use warnings'
  10.     # elsewhere as well, but *AFTER* 'use Regexp::Common'.
  11.     if ($] < 5.006) {
  12.         $INC {"warnings.pm"} = 1;
  13.         no strict 'refs';
  14.         *{"warnings::unimport"} = sub {0};
  15.     }
  16. }
  17.  
  18. use warnings;
  19. use vars qw /$VERSION %RE %sub_interface $AUTOLOAD/;
  20.  
  21. $VERSION = '2011121001';
  22.  
  23.  
  24. sub _croak {
  25.     require Carp;
  26.     goto &Carp::croak;
  27. }
  28.  
  29. sub _carp {
  30.     require Carp;
  31.     goto &Carp::carp;
  32. }
  33.  
  34. sub new {
  35.     my ($class, @data) = @_;
  36.     my %self;
  37.     tie %self, $class, @data;
  38.     return \%self;
  39. }
  40.  
  41. sub TIEHASH {
  42.     my ($class, @data) = @_;
  43.     bless \@data, $class;
  44. }
  45.  
  46. sub FETCH {
  47.     my ($self, $extra) = @_;
  48.     return bless ref($self)->new(@$self, $extra), ref($self);
  49. }
  50.  
  51. my %imports = map {$_ => "Regexp::Common::$_"}
  52.               qw /balanced CC     comment   delimited lingua list
  53.                   net      number profanity SEN       URI    whitespace
  54.                   zip/;
  55.  
  56. sub import {
  57.     shift;  # Shift off the class.
  58.     tie %RE, __PACKAGE__;
  59.     {
  60.         no strict 'refs';
  61.         *{caller() . "::RE"} = \%RE;
  62.     }
  63.  
  64.     my $saw_import;
  65.     my $no_defaults;
  66.     my %exclude;
  67.     foreach my $entry (grep {!/^RE_/} @_) {
  68.         if ($entry eq 'pattern') {
  69.             no strict 'refs';
  70.             *{caller() . "::pattern"} = \&pattern;
  71.             next;
  72.         }
  73.         # This used to prevent $; from being set. We still recognize it,
  74.         # but we won't do anything.
  75.         if ($entry eq 'clean') {
  76.             next;
  77.         }
  78.         if ($entry eq 'no_defaults') {
  79.             $no_defaults ++;
  80.             next;
  81.         }
  82.         if (my $module = $imports {$entry}) {
  83.             $saw_import ++;
  84.             eval "require $module;";
  85.             die $@ if $@;
  86.             next;
  87.         }
  88.         if ($entry =~ /^!(.*)/ && $imports {$1}) {
  89.             $exclude {$1} ++;
  90.             next;
  91.         }
  92.         # As a last resort, try to load the argument.
  93.         my $module = $entry =~ /^Regexp::Common/
  94.                             ? $entry
  95.                             : "Regexp::Common::" . $entry;
  96.         eval "require $module;";
  97.         die $@ if $@;
  98.     }
  99.  
  100.     unless ($saw_import || $no_defaults) {
  101.         foreach my $module (values %imports) {
  102.             next if $exclude {$module};
  103.             eval "require $module;";
  104.             die $@ if $@;
  105.         }
  106.     }
  107.  
  108.     my %exported;
  109.     foreach my $entry (grep {/^RE_/} @_) {
  110.         if ($entry =~ /^RE_(\w+_)?ALL$/) {
  111.             my $m  = defined $1 ? $1 : "";
  112.             my $re = qr /^RE_${m}.*$/;
  113.             while (my ($sub, $interface) = each %sub_interface) {
  114.                 next if $exported {$sub};
  115.                 next unless $sub =~ /$re/;
  116.                 {
  117.                     no strict 'refs';
  118.                     *{caller() . "::$sub"} = $interface;
  119.                 }
  120.                 $exported {$sub} ++;
  121.             }
  122.         }
  123.         else {
  124.             next if $exported {$entry};
  125.             _croak "Can't export unknown subroutine &$entry"
  126.                 unless $sub_interface {$entry};
  127.             {
  128.                 no strict 'refs';
  129.                 *{caller() . "::$entry"} = $sub_interface {$entry};
  130.             }
  131.             $exported {$entry} ++;
  132.         }
  133.     }
  134. }
  135.  
  136. sub AUTOLOAD { _croak "Can't $AUTOLOAD" }
  137.  
  138. sub DESTROY {}
  139.  
  140. my %cache;
  141.  
  142. my $fpat = qr/^(-\w+)/;
  143.  
  144. sub _decache {
  145.         my @args = @{tied %{$_[0]}};
  146.         my @nonflags = grep {!/$fpat/} @args;
  147.         my $cache = get_cache(@nonflags);
  148.         _croak "Can't create unknown regex: \$RE{"
  149.             . join("}{",@args) . "}"
  150.                 unless exists $cache->{__VAL__};
  151.         _croak "Perl $] does not support the pattern "
  152.             . "\$RE{" . join("}{",@args)
  153.             . "}.\nYou need Perl $cache->{__VAL__}{version} or later"
  154.                 unless ($cache->{__VAL__}{version}||0) <= $];
  155.         my %flags = ( %{$cache->{__VAL__}{default}},
  156.                       map { /$fpat\Q$;\E(.*)/ ? ($1 => $2)
  157.                           : /$fpat/           ? ($1 => undef)
  158.                           :                     ()
  159.                           } @args);
  160.         $cache->{__VAL__}->_clone_with(\@args, \%flags);
  161. }
  162.  
  163. use overload q{""} => \&_decache;
  164.  
  165.  
  166. sub get_cache {
  167.         my $cache = \%cache;
  168.         foreach (@_) {
  169.                 $cache = $cache->{$_}
  170.                       || ($cache->{$_} = {});
  171.         }
  172.         return $cache;
  173. }
  174.  
  175. sub croak_version {
  176.         my ($entry, @args) = @_;
  177. }
  178.  
  179. sub pattern {
  180.         my %spec = @_;
  181.         _croak 'pattern() requires argument: name => [ @list ]'
  182.                 unless $spec{name} && ref $spec{name} eq 'ARRAY';
  183.         _croak 'pattern() requires argument: create => $sub_ref_or_string'
  184.                 unless $spec{create};
  185.  
  186.         if (ref $spec{create} ne "CODE") {
  187.                 my $fixed_str = "$spec{create}";
  188.                 $spec{create} = sub { $fixed_str }
  189.         }
  190.  
  191.         my @nonflags;
  192.         my %default;
  193.         foreach ( @{$spec{name}} ) {
  194.                 if (/$fpat=(.*)/) {
  195.                         $default{$1} = $2;
  196.                 }
  197.                 elsif (/$fpat\s*$/) {
  198.                         $default{$1} = undef;
  199.                 }
  200.                 else {
  201.                         push @nonflags, $_;
  202.                 }
  203.         }
  204.  
  205.         my $entry = get_cache(@nonflags);
  206.  
  207.         if ($entry->{__VAL__}) {
  208.                 _carp "Overriding \$RE{"
  209.                    . join("}{",@nonflags)
  210.                    . "}";
  211.         }
  212.  
  213.         $entry->{__VAL__} = bless {
  214.                                 create  => $spec{create},
  215.                                 match   => $spec{match} || \&generic_match,
  216.                                 subs    => $spec{subs}  || \&generic_subs,
  217.                                 version => $spec{version},
  218.                                 default => \%default,
  219.                             }, 'Regexp::Common::Entry';
  220.  
  221.         foreach (@nonflags) {s/\W/X/g}
  222.         my $subname = "RE_" . join ("_", @nonflags);
  223.         $sub_interface{$subname} = sub {
  224.                 push @_ => undef if @_ % 2;
  225.                 my %flags = @_;
  226.                 my $pat = $spec{create}->($entry->{__VAL__},
  227.                                {%default, %flags}, \@nonflags);
  228.                 if (exists $flags{-keep}) { $pat =~ s/\Q(?k:/(/g; }
  229.                 else { $pat =~ s/\Q(?k:/(?:/g; }
  230.                 return exists $flags {-i} ? qr /(?i:$pat)/ : qr/$pat/;
  231.         };
  232.  
  233.         return 1;
  234. }
  235.  
  236. sub generic_match {$_ [1] =~  /$_[0]/}
  237. sub generic_subs  {$_ [1] =~ s/$_[0]/$_[2]/}
  238.  
  239. sub matches {
  240.         my ($self, $str) = @_;
  241.         my $entry = $self -> _decache;
  242.         $entry -> {match} -> ($entry, $str);
  243. }
  244.  
  245. sub subs {
  246.         my ($self, $str, $newstr) = @_;
  247.         my $entry = $self -> _decache;
  248.         $entry -> {subs} -> ($entry, $str, $newstr);
  249.         return $str;
  250. }
  251.  
  252.  
  253. package Regexp::Common::Entry;
  254. # use Carp;
  255.  
  256. use overload
  257.     q{""} => sub {
  258.         my ($self) = @_;
  259.         my $pat = $self->{create}->($self, $self->{flags}, $self->{args});
  260.         if (exists $self->{flags}{-keep}) {
  261.             $pat =~ s/\Q(?k:/(/g;
  262.         }
  263.         else {
  264.             $pat =~ s/\Q(?k:/(?:/g;
  265.         }
  266.         if (exists $self->{flags}{-i})   { $pat = "(?i)$pat" }
  267.         return $pat;
  268.     };
  269.  
  270. sub _clone_with {
  271.     my ($self, $args, $flags) = @_;
  272.     bless { %$self, args=>$args, flags=>$flags }, ref $self;
  273. }
  274.  
  275. 1;
  276.  
  277. __END__
  278.  
  279. =pod
  280.  
  281. =head1 NAME
  282.  
  283. Regexp::Common - Provide commonly requested regular expressions
  284.  
  285. =head1 SYNOPSIS
  286.  
  287.  # STANDARD USAGE 
  288.  
  289.  use Regexp::Common;
  290.  
  291.  while (<>) {
  292.      /$RE{num}{real}/               and print q{a number};
  293.      /$RE{quoted}/                  and print q{a ['"`] quoted string};
  294.      /$RE{delimited}{-delim=>'/'}/  and print q{a /.../ sequence};
  295.      /$RE{balanced}{-parens=>'()'}/ and print q{balanced parentheses};
  296.      /$RE{profanity}/               and print q{a #*@%-ing word};
  297.  }
  298.  
  299.  
  300.  # SUBROUTINE-BASED INTERFACE
  301.  
  302.  use Regexp::Common 'RE_ALL';
  303.  
  304.  while (<>) {
  305.      $_ =~ RE_num_real()              and print q{a number};
  306.      $_ =~ RE_quoted()                and print q{a ['"`] quoted string};
  307.      $_ =~ RE_delimited(-delim=>'/')  and print q{a /.../ sequence};
  308.      $_ =~ RE_balanced(-parens=>'()'} and print q{balanced parentheses};
  309.      $_ =~ RE_profanity()             and print q{a #*@%-ing word};
  310.  }
  311.  
  312.  
  313.  # IN-LINE MATCHING...
  314.  
  315.  if ( $RE{num}{int}->matches($text) ) {...}
  316.  
  317.  
  318.  # ...AND SUBSTITUTION
  319.  
  320.  my $cropped = $RE{ws}{crop}->subs($uncropped);
  321.  
  322.  
  323.  # ROLL-YOUR-OWN PATTERNS
  324.  
  325.  use Regexp::Common 'pattern';
  326.  
  327.  pattern name   => ['name', 'mine'],
  328.          create => '(?i:J[.]?\s+A[.]?\s+Perl-Hacker)',
  329.          ;
  330.  
  331.  my $name_matcher = $RE{name}{mine};
  332.  
  333.  pattern name    => [ 'lineof', '-char=_' ],
  334.          create  => sub {
  335.                         my $flags = shift;
  336.                         my $char = quotemeta $flags->{-char};
  337.                         return '(?:^$char+$)';
  338.                     },
  339.          match   => sub {
  340.                         my ($self, $str) = @_;
  341.                         return $str !~ /[^$self->{flags}{-char}]/;
  342.                     },
  343.          subs   => sub {
  344.                         my ($self, $str, $replacement) = @_;
  345.                         $_[1] =~ s/^$self->{flags}{-char}+$//g;
  346.                    },
  347.          ;
  348.  
  349.  my $asterisks = $RE{lineof}{-char=>'*'};
  350.  
  351.  # DECIDING WHICH PATTERNS TO LOAD.
  352.  
  353.  use Regexp::Common qw /comment number/;  # Comment and number patterns.
  354.  use Regexp::Common qw /no_defaults/;     # Don't load any patterns.
  355.  use Regexp::Common qw /!delimited/;      # All, but delimited patterns.
  356.  
  357.  
  358. =head1 DESCRIPTION
  359.  
  360. By default, this module exports a single hash (C<%RE>) that stores or generates
  361. commonly needed regular expressions (see L<"List of available patterns">).
  362.  
  363. There is an alternative, subroutine-based syntax described in
  364. L<"Subroutine-based interface">.
  365.  
  366.  
  367. =head2 General syntax for requesting patterns
  368.  
  369. To access a particular pattern, C<%RE> is treated as a hierarchical hash of
  370. hashes (of hashes...), with each successive key being an identifier. For
  371. example, to access the pattern that matches real numbers, you 
  372. specify:
  373.  
  374.         $RE{num}{real}
  375.         
  376. and to access the pattern that matches integers: 
  377.  
  378.         $RE{num}{int}
  379.  
  380. Deeper layers of the hash are used to specify I<flags>: arguments that
  381. modify the resulting pattern in some way. The keys used to access these
  382. layers are prefixed with a minus sign and may have a value; if a value
  383. is given, it's done by using a multidimensional key.
  384. For example, to access the pattern that
  385. matches base-2 real numbers with embedded commas separating
  386. groups of three digits (e.g. 10,101,110.110101101):
  387.  
  388.         $RE{num}{real}{-base => 2}{-sep => ','}{-group => 3}
  389.  
  390. Through the magic of Perl, these flag layers may be specified in any order
  391. (and even interspersed through the identifier keys!)
  392. so you could get the same pattern with:
  393.  
  394.         $RE{num}{real}{-sep => ','}{-group => 3}{-base => 2}
  395.  
  396. or:
  397.  
  398.         $RE{num}{-base => 2}{real}{-group => 3}{-sep => ','}
  399.  
  400. or even:
  401.  
  402.         $RE{-base => 2}{-group => 3}{-sep => ','}{num}{real}
  403.  
  404. etc.
  405.  
  406. Note, however, that the relative order of amongst the identifier keys
  407. I<is> significant. That is:
  408.  
  409.         $RE{list}{set}
  410.  
  411. would not be the same as:
  412.  
  413.         $RE{set}{list}
  414.  
  415. =head2 Flag syntax
  416.  
  417. In versions prior to 2.113, flags could also be written as
  418. C<{"-flag=value"}>. This no longer works, although C<{"-flag$;value"}>
  419. still does. However, C<< {-flag => 'value'} >> is the preferred syntax.
  420.  
  421. =head2 Universal flags
  422.  
  423. Normally, flags are specific to a single pattern.
  424. However, there is two flags that all patterns may specify.
  425.  
  426. =over 4
  427.  
  428. =item C<-keep>
  429.  
  430. By default, the patterns provided by C<%RE> contain no capturing
  431. parentheses. However, if the C<-keep> flag is specified (it requires
  432. no value) then any significant substrings that the pattern matches
  433. are captured. For example:
  434.  
  435.         if ($str =~ $RE{num}{real}{-keep}) {
  436.                 $number   = $1;
  437.                 $whole    = $3;
  438.                 $decimals = $5;
  439.         }
  440.  
  441. Special care is needed if a "kept" pattern is interpolated into a
  442. larger regular expression, as the presence of other capturing
  443. parentheses is likely to change the "number variables" into which significant
  444. substrings are saved.
  445.  
  446. See also L<"Adding new regular expressions">, which describes how to create
  447. new patterns with "optional" capturing brackets that respond to C<-keep>.
  448.  
  449. =item C<-i>
  450.  
  451. Some patterns or subpatterns only match lowercase or uppercase letters.
  452. If one wants the do case insensitive matching, one option is to use
  453. the C</i> regexp modifier, or the special sequence C<(?i)>. But if the
  454. functional interface is used, one does not have this option. The 
  455. C<-i> switch solves this problem; by using it, the pattern will do
  456. case insensitive matching.
  457.  
  458. =back
  459.  
  460. =head2 OO interface and inline matching/substitution
  461.  
  462. The patterns returned from C<%RE> are objects, so rather than writing:
  463.  
  464.         if ($str =~ /$RE{some}{pattern}/ ) {...}
  465.  
  466. you can write:
  467.  
  468.         if ( $RE{some}{pattern}->matches($str) ) {...}
  469.  
  470. For matching this would seem to have no great advantage apart from readability
  471. (but see below).
  472.  
  473. For substitutions, it has other significant benefits. Frequently you want to
  474. perform a substitution on a string without changing the original. Most people
  475. use this:
  476.  
  477.         $changed = $original;
  478.         $changed =~ s/$RE{some}{pattern}/$replacement/;
  479.  
  480. The more adept use:
  481.  
  482.         ($changed = $original) =~ s/$RE{some}{pattern}/$replacement/;
  483.  
  484. Regexp::Common allows you do write this:
  485.  
  486.         $changed = $RE{some}{pattern}->subs($original=>$replacement);
  487.  
  488. Apart from reducing precedence-angst, this approach has the added
  489. advantages that the substitution behaviour can be optimized from the 
  490. regular expression, and the replacement string can be provided by
  491. default (see L<"Adding new regular expressions">).
  492.  
  493. For example, in the implementation of this substitution:
  494.  
  495.         $cropped = $RE{ws}{crop}->subs($uncropped);
  496.  
  497. the default empty string is provided automatically, and the substitution is
  498. optimized to use:
  499.  
  500.         $uncropped =~ s/^\s+//;
  501.         $uncropped =~ s/\s+$//;
  502.  
  503. rather than:
  504.  
  505.         $uncropped =~ s/^\s+|\s+$//g;
  506.  
  507.  
  508. =head2 Subroutine-based interface
  509.  
  510. The hash-based interface was chosen because it allows regexes to be
  511. effortlessly interpolated, and because it also allows them to be
  512. "curried". For example:
  513.  
  514.         my $num = $RE{num}{int};
  515.  
  516.         my $commad     = $num->{-sep=>','}{-group=>3};
  517.         my $duodecimal = $num->{-base=>12};
  518.  
  519.  
  520. However, the use of tied hashes does make the access to Regexp::Common
  521. patterns slower than it might otherwise be. In contexts where impatience
  522. overrules laziness, Regexp::Common provides an additional
  523. subroutine-based interface.
  524.  
  525. For each (sub-)entry in the C<%RE> hash (C<$RE{key1}{key2}{etc}>), there
  526. is a corresponding exportable subroutine: C<RE_key1_key2_etc()>. The name of
  527. each subroutine is the underscore-separated concatenation of the I<non-flag>
  528. keys that locate the same pattern in C<%RE>. Flags are passed to the subroutine
  529. in its argument list. Thus:
  530.  
  531.         use Regexp::Common qw( RE_ws_crop RE_num_real RE_profanity );
  532.  
  533.         $str =~ RE_ws_crop() and die "Surrounded by whitespace";
  534.  
  535.         $str =~ RE_num_real(-base=>8, -sep=>" ") or next;
  536.  
  537.         $offensive = RE_profanity(-keep);
  538.         $str =~ s/$offensive/$bad{$1}++; "<expletive deleted>"/ge;
  539.  
  540. Note that, unlike the hash-based interface (which returns objects), these
  541. subroutines return ordinary C<qr>'d regular expressions. Hence they do not
  542. curry, nor do they provide the OO match and substitution inlining described
  543. in the previous section.
  544.  
  545. It is also possible to export subroutines for all available patterns like so:
  546.  
  547.         use Regexp::Common 'RE_ALL';
  548.  
  549. Or you can export all subroutines with a common prefix of keys like so:
  550.  
  551.         use Regexp::Common 'RE_num_ALL';
  552.  
  553. which will export C<RE_num_int> and C<RE_num_real> (and if you have
  554. create more patterns who have first key I<num>, those will be exported
  555. as well). In general, I<RE_key1_..._keyn_ALL> will export all subroutines
  556. whose pattern names have first keys I<key1> ... I<keyn>.
  557.  
  558.  
  559. =head2 Adding new regular expressions
  560.  
  561. You can add your own regular expressions to the C<%RE> hash at run-time,
  562. using the exportable C<pattern> subroutine. It expects a hash-like list of 
  563. key/value pairs that specify the behaviour of the pattern. The various
  564. possible argument pairs are:
  565.  
  566. =over 4
  567.  
  568. =item C<name =E<gt> [ @list ]>
  569.  
  570. A required argument that specifies the name of the pattern, and any
  571. flags it may take, via a reference to a list of strings. For example:
  572.  
  573.          pattern name => [qw( line of -char )],
  574.                  # other args here
  575.                  ;
  576.  
  577. This specifies an entry C<$RE{line}{of}>, which may take a C<-char> flag.
  578.  
  579. Flags may also be specified with a default value, which is then used whenever
  580. the flag is specified without an explicit value (but not when the flag is
  581. omitted). For example:
  582.  
  583.          pattern name => [qw( line of -char=_ )],
  584.                  # default char is '_'
  585.                  # other args here
  586.                  ;
  587.  
  588.  
  589. =item C<create =E<gt> $sub_ref_or_string>
  590.  
  591. A required argument that specifies either a string that is to be returned
  592. as the pattern:
  593.  
  594.         pattern name    => [qw( line of underscores )],
  595.                 create  => q/(?:^_+$)/
  596.                 ;
  597.  
  598. or a reference to a subroutine that will be called to create the pattern:
  599.  
  600.         pattern name    => [qw( line of -char=_ )],
  601.                 create  => sub {
  602.                                 my ($self, $flags) = @_;
  603.                                 my $char = quotemeta $flags->{-char};
  604.                                 return '(?:^$char+$)';
  605.                             },
  606.                 ;
  607.  
  608. If the subroutine version is used, the subroutine will be called with 
  609. three arguments: a reference to the pattern object itself, a reference
  610. to a hash containing the flags and their values,
  611. and a reference to an array containing the non-flag keys. 
  612.  
  613. Whatever the subroutine returns is stringified as the pattern.
  614.  
  615. No matter how the pattern is created, it is immediately postprocessed to
  616. include or exclude capturing parentheses (according to the value of the
  617. C<-keep> flag). To specify such "optional" capturing parentheses within
  618. the regular expression associated with C<create>, use the notation
  619. C<(?k:...)>. Any parentheses of this type will be converted to C<(...)>
  620. when the C<-keep> flag is specified, or C<(?:...)> when it is not.
  621. It is a Regexp::Common convention that the outermost capturing parentheses
  622. always capture the entire pattern, but this is not enforced.
  623.  
  624.  
  625. =item C<match =E<gt> $sub_ref>
  626.  
  627. An optional argument that specifies a subroutine that is to be called when
  628. the C<$RE{...}-E<gt>matches(...)> method of this pattern is invoked.
  629.  
  630. The subroutine should expect two arguments: a reference to the pattern object
  631. itself, and the string to be matched against.
  632.  
  633. It should return the same types of values as a C<m/.../> does.
  634.  
  635.      pattern name    => [qw( line of -char )],
  636.              create  => sub {...},
  637.              match   => sub {
  638.                              my ($self, $str) = @_;
  639.                              $str !~ /[^$self->{flags}{-char}]/;
  640.                         },
  641.              ;
  642.  
  643.  
  644. =item C<subs =E<gt> $sub_ref>
  645.  
  646. An optional argument that specifies a subroutine that is to be called when
  647. the C<$RE{...}-E<gt>subs(...)> method of this pattern is invoked.
  648.  
  649. The subroutine should expect three arguments: a reference to the pattern object
  650. itself, the string to be changed, and the value to be substituted into it.
  651. The third argument may be C<undef>, indicating the default substitution is
  652. required.
  653.  
  654. The subroutine should return the same types of values as an C<s/.../.../> does.
  655.  
  656. For example:
  657.  
  658.      pattern name    => [ 'lineof', '-char=_' ],
  659.              create  => sub {...},
  660.              subs    => sub {
  661.                           my ($self, $str, $ignore_replacement) = @_;
  662.                           $_[1] =~ s/^$self->{flags}{-char}+$//g;
  663.                         },
  664.              ;
  665.  
  666. Note that such a subroutine will almost always need to modify C<$_[1]> directly.
  667.  
  668.  
  669. =item C<version =E<gt> $minimum_perl_version>
  670.  
  671. If this argument is given, it specifies the minimum version of perl required
  672. to use the new pattern. Attempts to use the pattern with earlier versions of
  673. perl will generate a fatal diagnostic.
  674.  
  675. =back
  676.  
  677. =head2 Loading specific sets of patterns.
  678.  
  679. By default, all the sets of patterns listed below are made available.
  680. However, it is possible to indicate which sets of patterns should
  681. be made available - the wanted sets should be given as arguments to
  682. C<use>. Alternatively, it is also possible to indicate which sets of
  683. patterns should not be made available - those sets will be given as
  684. argument to the C<use> statement, but are preceded with an exclaimation
  685. mark. The argument I<no_defaults> indicates none of the default patterns
  686. should be made available. This is useful for instance if all you want
  687. is the C<pattern()> subroutine.
  688.  
  689. Examples:
  690.  
  691.  use Regexp::Common qw /comment number/;  # Comment and number patterns.
  692.  use Regexp::Common qw /no_defaults/;     # Don't load any patterns.
  693.  use Regexp::Common qw /!delimited/;      # All, but delimited patterns.
  694.  
  695. It's also possible to load your own set of patterns. If you have a
  696. module C<Regexp::Common::my_patterns> that makes patterns available,
  697. you can have it made available with
  698.  
  699.  use Regexp::Common qw /my_patterns/;
  700.  
  701. Note that the default patterns will still be made available - only if
  702. you use I<no_defaults>, or mention one of the default sets explicitly,
  703. the non mentioned defaults aren't made available.
  704.  
  705. =head2 List of available patterns
  706.  
  707. The patterns listed below are currently available. Each set of patterns
  708. has its own manual page describing the details. For each pattern set
  709. named I<name>, the manual page I<Regexp::Common::name> describes the
  710. details.
  711.  
  712. Currently available are:
  713.  
  714. =over 4
  715.  
  716. =item Regexp::Common::balanced
  717.  
  718. Provides regexes for strings with balanced parenthesized delimiters.
  719.  
  720. =item Regexp::Common::comment
  721.  
  722. Provides regexes for comments of various languages (43 languages
  723. currently).
  724.  
  725. =item Regexp::Common::delimited
  726.  
  727. Provides regexes for delimited strings.
  728.  
  729. =item Regexp::Common::lingua
  730.  
  731. Provides regexes for palindromes.
  732.  
  733. =item Regexp::Common::list
  734.  
  735. Provides regexes for lists.
  736.  
  737. =item Regexp::Common::net
  738.  
  739. Provides regexes for IPv4 addresses and MAC addresses.
  740.  
  741. =item Regexp::Common::number
  742.  
  743. Provides regexes for numbers (integers and reals).
  744.  
  745. =item Regexp::Common::profanity
  746.  
  747. Provides regexes for profanity.
  748.  
  749. =item Regexp::Common::whitespace
  750.  
  751. Provides regexes for leading and trailing whitespace.
  752.  
  753. =item Regexp::Common::zip
  754.  
  755. Provides regexes for zip codes.
  756.  
  757. =back
  758.  
  759. =head2 Forthcoming patterns and features
  760.  
  761. Future releases of the module will also provide patterns for the following:
  762.  
  763.         * email addresses 
  764.         * HTML/XML tags
  765.         * more numerical matchers,
  766.         * mail headers (including multiline ones),
  767.         * more URLS
  768.         * telephone numbers of various countries
  769.         * currency (universal 3 letter format, Latin-1, currency names)
  770.         * dates
  771.         * binary formats (e.g. UUencoded, MIMEd)
  772.  
  773. If you have other patterns or pattern generators that you think would be
  774. generally useful, please send them to the maintainer -- preferably as source
  775. code using the C<pattern> subroutine. Submissions that include a set of
  776. tests will be especially welcome.
  777.  
  778.  
  779. =head1 DIAGNOSTICS
  780.  
  781. =over 4
  782.  
  783. =item C<Can't export unknown subroutine %s>
  784.  
  785. The subroutine-based interface didn't recognize the requested subroutine.
  786. Often caused by a spelling mistake or an incompletely specified name.
  787.  
  788.         
  789. =item C<Can't create unknown regex: $RE{...}>
  790.  
  791. Regexp::Common doesn't have a generator for the requested pattern.
  792. Often indicates a misspelt or missing parameter.
  793.  
  794. =item
  795. C<Perl %f does not support the pattern $RE{...}.
  796. You need Perl %f or later>
  797.  
  798. The requested pattern requires advanced regex features (e.g. recursion)
  799. that not available in your version of Perl. Time to upgrade.
  800.  
  801. =item C<< pattern() requires argument: name => [ @list ] >>
  802.  
  803. Every user-defined pattern specification must have a name.
  804.  
  805. =item C<< pattern() requires argument: create => $sub_ref_or_string >>
  806.  
  807. Every user-defined pattern specification must provide a pattern creation
  808. mechanism: either a pattern string or a reference to a subroutine that
  809. returns the pattern string.
  810.  
  811. =item C<Base must be between 1 and 36>
  812.  
  813. The C<< $RE{num}{real}{-base=>'I<N>'} >> pattern uses the characters [0-9A-Z]
  814. to represent the digits of various bases. Hence it only produces
  815. regular expressions for bases up to hexatricensimal.
  816.  
  817. =item C<Must specify delimiter in $RE{delimited}>
  818.  
  819. The pattern has no default delimiter.
  820. You need to write: C<< $RE{delimited}{-delim=>I<X>'} >> for some character I<X>
  821.  
  822. =back
  823.  
  824. =head1 ACKNOWLEDGEMENTS
  825.  
  826. Deepest thanks to the many people who have encouraged and contributed to this
  827. project, especially: Elijah, Jarkko, Tom, Nat, Ed, and Vivek.
  828.  
  829. Further thanks go to: Alexandr Ciornii, Blair Zajac, Bob Stockdale,
  830. Charles Thomas, Chris Vertonghen, the CPAN Testers, David Hand,
  831. Fany, Geoffrey Leach, Hermann-Marcus Behrens, Jerome Quelin, Jim Cromie,
  832. Lars Wilke, Linda Julien, Mike Arms, Mike Castle, Mikko, Murat Uenalan,
  833. RafaE<235>l Garcia-Suarez, Ron Savage, Sam Vilain, Slaven Rezic, Smylers,
  834. Tim Maher, and all the others I've forgotten.
  835.  
  836. =head1 AUTHOR
  837.  
  838. Damian Conway (damian@conway.org)
  839.  
  840. =head1 MAINTAINANCE
  841.  
  842. This package is maintained by Abigail S<(I<regexp-common@abigail.be>)>.
  843.  
  844. =head1 BUGS AND IRRITATIONS
  845.  
  846. Bound to be plenty.
  847.  
  848. For a start, there are many common regexes missing.
  849. Send them in to I<regexp-common@abigail.be>.
  850.  
  851. There are some POD issues when installing this module using a pre-5.6.0 perl;
  852. some manual pages may not install, or may not install correctly using a perl
  853. that is that old. You might consider upgrading your perl.
  854.  
  855. =head1 NOT A BUG
  856.  
  857. =over 4
  858.  
  859. =item *
  860.  
  861. The various patterns are not anchored. That is, a pattern like 
  862. C<< $RE {num} {int} >> will match against "abc4def", because a 
  863. substring of the subject matches. This is by design, and not a
  864. bug. If you want the pattern to be anchored, use something like:
  865.  
  866.  my $integer = $RE {num} {int};
  867.  $subj =~ /^$integer$/ and print "Matches!\n";
  868.  
  869. =back
  870.  
  871. =head1 LICENSE and COPYRIGHT
  872.  
  873. This software is Copyright (c) 2001 - 2011, Damian Conway and Abigail.
  874.  
  875. This module is free software, and maybe used under any of the following
  876. licenses:
  877.  
  878.  1) The Perl Artistic License.     See the file COPYRIGHT.AL.
  879.  2) The Perl Artistic License 2.0. See the file COPYRIGHT.AL2.
  880.  3) The BSD Licence.               See the file COPYRIGHT.BSD.
  881.  4) The MIT Licence.               See the file COPYRIGHT.MIT.
  882.