Skip to content

Instantly share code, notes, and snippets.

@csirac2
Created April 16, 2011 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csirac2/923055 to your computer and use it in GitHub Desktop.
Save csirac2/923055 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -wT
use warnings;
use strict;
sub _stashText {
my ($fragment, $map, $key) = @_;
my $id = $map->{index};
ASSERT(ref($map) eq 'HASH') if DEBUG;
ASSERT(defined $key) if DEBUG;
ASSERT(defined $fragment) if DEBUG;
$map->{$id} = $fragment;
$map->{index} += 1;
return "\01takeOut$key $id\02";
}
sub _takeOutText {
my ($text, $regex, $map, $key) = @_;
ASSERT(ref($map) eq 'HASH') if DEBUG;
ASSERT(defined $regex);
$key ||= '';
$map->{index} = 0;
if (ref($text) eq 'SCALAR') {
${$text} =~ s/$regex/_stashText($1, $map, $key)/gems;
} else {
ASSERT(not ref($text)) if DEBUG;
$text =~ s/$regex/_stashText($1, $map, $key)/gems;
}
return $text;
}
sub _putBackText {
my ($ptext, $map, $key) = @_;
ASSERT(ref($map) eq 'HASH') if DEBUG;
$key ||= '';
if (ref($ptext) eq 'SCALAR') {
${$ptext} =~ s/\01takeOut$key (\d+)\02/$map->{$1}/gems;
} else {
ASSERT(not ref($ptext)) if DEBUG;
$ptext =~ s/\01takeOut$key (\d+)\02/$map->{$1}/gems;
}
return $ptext;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment