且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在替换中使用变量作为修饰符

更新时间:2023-11-24 23:12:04

嗯,如果必须这样做,我会这样做:

Hm, if I had to do it I would do like this:

use warnings;
use strict;
my @stuff = (
{
    search => "this",
    replace => "that",
    modifier => "g",
},
{
    search => "ono",
    replace => "wendy",
    modifier => "i",
}
);
$_ = "this ono boo this\n";
for my $h (@stuff) {
    if ($h->{modifier} eq 'g') {
        s/$h->{search}/$h->{replace}/g;
    } elsif ($h->{modifier} eq 'i') {
        s/$h->{search}/$h->{replace}/i;
    }
    # etc.
}
print;

您可能只想使用许多不同的修饰符,所以我认为这很容易.

There are only so many different modifiers you might want to use so I think this is easy enough.

您可以使用eval来完成此操作,但是它非常混乱.

You can use eval for this, but it's awfully messy.