#!/usr/bin/perl -w use strict; while () { next unless /^\s*s\//; # Minimal checking chomp; my ($pat, $repl) = (split '/') [1, 2]; my $s = "foo"; eval "\$s =~ s/$pat/$repl/"; warn "Warning: $@" if $@; print "$s\n"; } __DATA__ s/foo/bar/ s/(fo)o/bar$1/