Ich arbeite an einem Perl-CGI-Skript. Es sollte eine Art Datenbank verarbeiten, in der Drucklayouts und die zu druckenden Werte in einem Hash gespeichert werden. Hier kommt mein Code-Schnipsel:Interpolation von Hash-Format-String
my $va=1;
my $vb=2;
my $fa="";
my $fb="";
my %h=('format' => { 'a' => "This is the value of variable \$va: $va",
'b' => "This is the value of variable \$vb: $vb"
},
'values' => { 'a' => "value A",
'b' => "value B"
}
);
$fa=$h{'format'}->{'a'};
$fb=$h{'format'}->{'b'};
$va=$h{'values'}->{'a'};
$vb=$h{'values'}->{'b'};
print "$fa<br/>\n";
print "$fb<br/>\n";
$va=3;
$vb=4;
print "$fa<br/>\n";
print "$fb<br/>\n";
Bis jetzt habe ich nur diese (falsche) Ausdruck:
This is the value of variable $va: 1
This is the value of variable $vb: 2
This is the value of variable $va: 1
This is the value of variable $vb: 2
Was erwarte ich tun ist:
This is the value of variable $va: value A
This is the value of variable $vb: value B
This is the value of variable $va: 3
This is the value of variable $vb: 4
Was könnte der Grund sein, dass Die Interpolation der im Hash gespeicherten Format-Strings funktioniert nicht?
Die Interpolation passiert, wenn der Hash initialisiert wird ('my% h ='), nicht wenn er aufgerufen wird, also '$ va' ist immer noch 1 und' $ vb' ist 2. Dies scheint ein [XY Problem] (http://meta.stackexchange.com/q/66377/234299), aber ... kannst du bitte mehr über das, was du versuchst, erklären? – ThisSuitIsBlackNot
@ThisSuitIsBlackNot Ja Sie haben Recht. Ich habe jetzt die Doppelquoten von ... 'a' => "..." zu 'a' => '...' geändert. Der Hash ist nun nicht interpolierbar und wird so ohne Interpolation initialisiert. Aber der Ausdruck gibt: Dies ist der Wert der Variablen \ $ va: $ va Dies ist der Wert der Variable \ $ vb: $ vb Auch nicht interpoliert - warum? –
Auf den ersten Blick denke ich "eval" ist ein Schlüsselwort hier ... – PerlDuck