In an eval evironment in which  @_ was, e.g.,
    @_ = ('searchPattern', 'replacementString', 'regexModifiers');
(presumably, the eval statement is called in the context of a function to which arguments were passed via @_), the eval would return a code reference equivalent to
    sub { s/searchPattern/replacementString/regexModifiers for @_ }
which would allow one to iterate over a list of strings and do substitutions on each (non-literal!) string:

my $x = 'some'; my $y = 'strings'; my $z = 'here'; $sub->($x, $y, $z);

The only benefit that I can see of using the string eval is that the regex | substitution operator modifiers  /g /e /r and perhaps a couple others cannot be "passed" in any other way. If not for these modifiers, one could simply write something like (again, assuming this is in a function in which  @_ held search/replace strings)
    my $sub = sub { s/$_->[0]/$_->[1]/xmsg for @_ };
    ...
    $sub->($x, $y, $z);
(note the literal modifier group) and get the same result. In this example, the modifiers other than  /g could be passed in a string.

Update: E.g.,

c:\@Work\Perl\monks>perl -wMstrict -le "S('(?xmsi) foo', 'bar', 'g'); ;; sub S { my $sub = eval qq{ sub { s/$_[0]/$_[1]/$_[2] for \@_ } }; ;; my $x = 'foo'; my $y = 'Foo fOo foO'; my $z = 'FOO'; ;; $sub->($x, $y, $z); print qq{x '$x' y '$y' z '$z'}; } " x 'bar' y 'bar bar bar' z 'bar'
And yes, this is a trick for a completely trusted environment. But then: "Trust, but verify!"


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Precompiling substitution regex by AnomalousMonk
in thread Precompiling substitution regex by FreakyGreenLeaky

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.