in reply to unconventional string substitutions

Have a look at eval. Something like this (untested) should work as you desire. I don't think you need the block eval but do not have the ability to test. eval has security implications.

eval{ eval "(\$output = \$string) =~ s/\$regexp/\$new/$suffix" };

Replies are listed 'Best First'.
Re^2: unconventional string substitutions
by ikegami (Patriarch) on Mar 20, 2008 at 05:31 UTC

    The outer eval is indeed not needed. Both forms of eval catch exceptions.

    As long as $suffix is constrained to /^[xismg]+\z/, there should be no security issues from the eval. There are, however, security issues with regexes, though. The following should be a good *start*:

    my $string = ...; my $regexp = ...; my $new = ...; my $suffix = ...; my $timeout = 2; my $output; $suffix =~ /^[xismg]+\z/ or die("Bad suffix\n"); my $sop = eval "sub { s/\$regexp/\$new/$suffix }"; or die("Bad regexp: $@\n"); eval { local $SIG{ALRM} = sub { die "Alarm\n" }; alarm($timeout); $sop->() for $output = $string; 1 } or die("Timeout\n");

    The denial of service isn't completely addressed. Even though the search is limited to $timeout seconds, the user can send requests faster than once a second.

    The security risk from bugs in the regexp engine isn't addressed at all.

Re^2: unconventional string substitutions
by halfcountplus (Hermit) on Mar 20, 2008 at 03:48 UTC