Maybe you can just run the substitution and limit it by time and/or number of replacements?
Here's a quick'n dirty example:

use strict; use warnings; { my $count; my $timeout; sub set_count { $count = shift; } sub set_timeout { $timeout = $_[0] ? time() + $_[0] : 0; } sub limited_subst { my ($from, $replacement) = @_; die "Too many replacements!" unless $count--; die "Time is up!" if $timeout and time() > $timeout; sleep(1); # just for demonstration! # warn "Replacing $from -> $replacement\n"; return $replacement; } } sub test_replace { my ($string, $from, $replacement, $limit, $timeout_epoch) = @_; my $rollback = $string; set_count( $limit ); set_timeout( $timeout_epoch ); eval { $string =~ s/ ($from) / limited_subst($1, $replacement ) /gxe; }; if ($@) { print "Oops! $@"; $string = $rollback; } return $string; } my $string = "la " x 30; print "Checking with no real limit:\n"; print "in : $string\nout: ", test_replace($string, 'la', 'ho!', -1, 0) +, "\n\n"; print "Checking with subst_limit 3:\n"; print "in : $string\nout: ", test_replace($string, 'la', 'ho!', 3, 0) +, "\n\n"; print "Checking with timeout 3:\n"; print "in : $string\nout :", test_replace($string, 'la', 'ho!', 30000, + 3), "\n\n"; __END__ Output: Checking with no real limit: in : la la la la la la la la la la la la la la la la la la la la la la + la la la la la la la la out: ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! h +o! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! ho! Checking with subst_limit 3: Oops! Too many replacements! at 910736.pl line 15. in : la la la la la la la la la la la la la la la la la la la la la la + la la la la la la la la out: la la la la la la la la la la la la la la la la la la la la la la + la la la la la la la la Checking with timeout 3: Oops! Time is up! at 910736.pl line 16. in : la la la la la la la la la la la la la la la la la la la la la la + la la la la la la la la out :la la la la la la la la la la la la la la la la la la la la la la + la la la la la la la la
Another idea: Maybe, you can start a sub-process that operates in a limited shell?
Important: I didn't scrutinized this for security holes!


In reply to Re^3: safe method to execute perl code with user input? by Perlbotics
in thread safe method to execute perl code with user input? by Allasso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.