Ben Bullock > Regexp-Subst-NoRegex-0.02 > Regexp::Subst::NoRegex

Download:
Regexp-Subst-NoRegex-0.02.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 0.02   Source  

NAME ^

Regexp::Subst::NoRegex - emulate s/// using s/\Q// or substr

SYNOPSIS ^

  use Regexp::Subst::NoRegex qw/rnr_substr rnr_sop/;
  my $text = "ajim jam jom ejeme";
  my $copy = $text;
  $copy =~ s/\bj(.)(.)/$2 $1/g;
  my $copy1 = rnr_substr ($text, '\bj(.)(.)', '$2 $1');
  my $copy2 = rnr_sop ($text, '\bj(.)(.)', '$2 $1');
  if ($copy eq $copy2 && $copy eq $copy1) {
      print "OK\n";
  }

DESCRIPTION ^

Given $text, a regex $left and a right hand side $right, perform the substitution $text =~ s/$left/$right/g; without using regular expressions for the substitution operation.

The module contains two different algorithms. One, rnr_substr, uses substr to perform the substitutions, and the other, rnr_sop, uses multiple non-regex substitutions of the form $text =~ /\Q$x/$y/ to emulate Perl's regex substitution while actually switching it off with the \Q.

EXPORT

Exports rnr_substr (substr version) and rnr_sop (s/\Q// version) on request.

$verbose

Set

  $Regexp::Subst::NoRegex::verbose = 1;

to see what the module is doing.

rnr_substr

Regular expression substitution without using any regular expressions (rnr stands for regex no regex) - emulate

  $text =~ s/$left/$right/g;

with a series of substitutions which use Perl's built in function substr

  substr ($text, $position, $length) = $y;
rnr_substr ($text, $left, $right);
text

The text you want to substitute

left

Any LHS expression, including regular expressions.

right

Any RHS expression, including things like $1, $2, etc.

rnr_sop

Regular expression substitution without using any regular expressions (rnr stands for regex no regex) - emulate

  $text =~ s/$left/$right/g;

with the subsitution operator (s///) with regexes switched off.

  $text =~ s/\Q$x/$y/g;
rnr_sop ($text, $left, $right, $subsop);
subsop

This is a hash reference containing

$subsop{global}

callback routine for global substitutions

Takes arguments ($data, $left, $right) where $left should be globally substituted for $right, and $data is the data from $subsop{data}.

$subsop{single}

callback routine for sequential substitutions

Takes arguments ($data, $left, $right, $orig, $gb_list);

where $gb_list is a list of good or bad substitutions in the form ('good', 'bad', 'bad', 'good', 'good'), and $left should be substituted with either $right or $orig depending on whether $gb_list says "good" or "bad". $data is $subsop{data}.

$subsop{data}

data to send to the callback routines.

AUTHOR ^

Ben Bullock, <benkasminbullock@gmail.com<gt>

COPYRIGHT AND LICENSE ^

Copyright (C) 2008 by Ben Kasmin Bullock.

This module is distributed under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.