In response to your updated question,

my $name = '(\w[\w-]*\w?)'; # any valid hostname segment my $nnam = "(($name\\.)*$name)"; # one or more "name" segments my %domainnames = ( "$nname\\.com" => 'a commercial domain', "$nname\\.edu" => 'an educational institution', );

is a solution. When quoting that with double quotes, you need to escape $ (unless you want interpolation), @ (unless you want interpolation), " and \ by preceeding them with \, so the regexp is $nname\.com would be "$nname\\.com" as a string literal.

But it's simpler to use qr/.../ (like I suggested in my earlier post):

my $name = qr/(\w[\w-]*\w?)/; # any valid hostname segment my $nnam = qr/(($name\.)*$name)/; # one or more "name" segments my %domainnames = ( qr/$nname\.com/ => 'a commercial domain', qr/$nname\.edu/ => 'an educational institution', );

However, it's a big waste to stringify a compiled regexp, so you might want to alter your structure for a major speed boost. I doubt you actually need a hash, so here's a solution using an array:

my $name = qr/(\w[\w-]*\w?)/; # any valid hostname segment my $nnam = qr/(($name\.)*$name)/; # one or more "name" segments my @domainnames = ( { re => qr/$nname\.com/, desc => 'a commercial domain' }, { re => qr/$nname\.edu/, desc => 'an educational institution' }, );

If you really do need a hash:

my $name = qr/(\w[\w-]*\w?)/; # any valid hostname segment my $nnam = qr/(($name\.)*$name)/; # one or more "name" segments my @domainnames = ( qr/$nname\.com/ => 'a commercial domain', qr/$nname\.edu/ => 'an educational institution', ); my %domainnames; foreach (0 .. @domainnames/2-1) { $domainnames{$domainnames[$_*2+0]} = { re => $domainnames[$_*2+0], desc => $domainnames[$_*2+1], }; }

In reply to Re: variable interpolation sans character interpolation by ikegami
in thread variable interpolation sans character interpolation by vacant

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.