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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.