【发布时间】:2023-03-24 06:16:01
【问题描述】:
我有一个路径名,其中包含我想替换的占位符:
# an example path with a placeholder
my $path = '%myproject%Web/ui/images/';
# mapping of all placeholders
my %placeholders = (
myproject => 'myproject/installation/all'
);
# substituting all placeholders in the path
$path =~ s!%(.*?)%!/$placeholders{$1}/!g;
# works fine -> 'myproject/installation/all/Web/ui/images/'
print $path;
这段代码运行良好,但有一个问题:我有一长串文件名和指定的不同占位符(因此是哈希)。现在,为了更加稳健,如果路径中指定的占位符在 %placeholders 映射中不存在,我想抛出一个错误。
有没有办法做到这一点?
【问题讨论】:
标签: regex perl substitution