#!/usr/bin/perl use warnings; use strict; use Benchmark qw(cmpthese); sub str_replace { my ($str, $to_match, $replace_with) = @_; my $pos = 0; my ($lm, $lr) = map { length; } ($to_match, $replace_with); while ( (my $i = index($str, $to_match, $pos)) != -1) { substr $str, $i, $lm, $replace_with; $pos = $i + $lr; } return $str; } sub str_replace2 { my ($str, $to_match, $replace_with) = @_; $str =~ s/$to_match/$replace_with/g; return $str; } my $str_main = < \&a, 'regex' => \&b });