Something like this? No eval necessary.
use warnings; use strict; sub dive { my ($r, @p) = @_; return unless @p; unshift @p, 'default' if @p==1; $r = ref $r eq 'HASH' && exists $$r{$_} ? $$r{$_} : return for + @p; return $r } my %var = ( default => { foo => 'Default-Foo' }, foo => { bar => 'Foo-Bar' }, a => { deeply => { nested => { value => 'A-Deeply-Nested-Value +' } } }, ); my $work = <<'EOF'; Hello, %foo Blah, %foo.bar Deep: %a.deeply.nested.value EOF print $work =~ s{ % ( \w+ (?:\.\w+)* ) }{ dive \%var, split /\./, $1 } +xegr; __END__ Hello, Default-Foo Blah, Foo-Bar Deep: A-Deeply-Nested-Value
This is based on my node here. I also have similar code linked from my scratchpad in the bullet point "Data::Diver type code".
In reply to Re: Recursively substitute against multidimensional hash
by haukex
in thread Recursively substitute against multidimensional hash
by Maelstrom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |