NAME

Regexp::MatchContext - Replace (and improve) $MATCH, $PREMATCH, and $POSTMATCH

VERSION

This document describes Regexp::MatchContext version 0.0.2

SYNOPSIS

  use Regexp::MatchContext -vars;

  $str = m/(?p) \d+ /;

  print "Before:  $PREMATCH\n";
  print "Matched: $MATCH\n";
  print "After:   $POSTMATCH\n";

  $MATCH = 2 * $MATCH;      # substitute into original $str

DESCRIPTION

The English.pm module provides named aliases for Perl's built-in $`, $& and $' variables: $PREMATCH, $MATCH, and $POSTMATCH. Unfortunately, those aliases suffer the same problems as their originals: they degrade the performance of every single regex in your program, even if you're only using them to get information about a single match.

This module also provides $PREMATCH, $MATCH, and $POSTMATCH, but in a way that only impacts the performance of matches that you specify. That is, these three variables are only set if the most recently matched regex contained the special (non-standard) meta- flag: (?p).

That is:

use Regexp::MatchContext -vars;

$str = 'foobarbaz';

$str =~ /(?p) foo /x;

# $PREMATCH contains 'foo'
# $MATCH contains 'bar'
# $POSTMATCH contains 'baz'

$str =~ / foo /x;

# $PREMATCH, $MATCH, and $POSTMATCH all undef

The (?p) marker can be placed anywhere within the regex and, except for setting the three context variables on a successful match, is otherwise totally ignored.

Lvalue match variables

Unlike the match variables provided in standard Perl, all three match variables provided by Regexp::MatchContext are actually aliases into the original string that the preceding regex matched. So assigning to any of these variables, changes the original string.

This means that, instead of:

$str =~ s/ foo /bar/;

you could write:

$str =~ m/ foo (?p)/;

$MATCH = 'bar';

Or remove everything before a match:

$str =~ m/ foo (?p)/;

$PREMATCH = q{};

Match subroutines

If you load the module with the argument -subs instead of -vars, then instead of exporting the variables $PREMATCH, $MATCH, and $POSTMATCH, it exports three subroutines: PREMATCH(), MATCH(), and POSTMATCH(). The subroutines are provided for those who prefer not to use package variables in their code.

Calling any of these subroutines returns the same value that the corresponding variable would have yielded:

use Regexp::MatchContext -subs;

$str = 'foobarbaz';

$str =~ /(?p) foo /x;

# PREMATCH() returns 'foo'
# MATCH() returns 'bar'
# POSTMATCH() returns 'baz'

$str =~ / foo /x;

# PREMATCH(), MATCH(), and POSTMATCH() all return undef

The three subroutines are also declared :lvalue, so that calls to them can be assigned to, which causes the corresponding part of the matched string to be changed (just as assignments to the equivalent variables does). For example:

$str =~ m/ foo (?p)/;

MATCH() = 'bar';

PREMATCH() = q{};

DIAGNOSTICS

Can't assign to %s because the preceding match didn't set it.

You tried to assign back via one of the capture variables, but the preceding regex didn't capture anything, so the assignment would do no good. Did you forget to put a (?p) in your regex?

CONFIGURATION AND ENVIRONMENT

Regexp::MatchContext requires no configuration files or environment variables.

DEPENDENCIES

Depends on the Tie::StdScalar and version modules.

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-regexp-matchcontext@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Damian Conway <DCONWAY@cpan.org>

LICENCE AND COPYRIGHT

Copyright (c) 2005, Damian Conway <DCONWAY@cpan.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.