我有一个带有占位符的路径名,我想替换它:
# 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
现在,为了更加稳健,如果路径中指定的占位符在映射中不存在,我想抛出一个错误。
有没有办法做到这一点?