I have a pathname with placeholders in it which i want to substitute:
# 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;
This code works fine, but has one problem: I have a long list of filesnames and different placeholders specified (hence the hash). Now for the sake of more robustness I’d like to throw an error if there is a placeholder specified in the path which does not exist in the %placeholders mapping.
Is there a way to achieve that?
This can be done very easily using
/e:Note: I think it’s good to replace
.*?with[^%]+to prevent incorrect matching.