in reply to variable interpolation sans character interpolation
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], }; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: variable interpolation sans character interpolation
by vacant (Pilgrim) on Oct 28, 2005 at 03:05 UTC | |
by ikegami (Patriarch) on Oct 28, 2005 at 04:28 UTC |