Why not use an intermediate variable?
my $common_value = 'Something';
my %some_hash = ( k1 => $common_value, k2 => $common_value.'Else' );
Update
kemp asked why an intermediate variable was needed. I can interpret this question in two ways, I will try to answer both interpretations.
Why can't you do my %hash = ( k1 => 'foo', k2 => $hash{k1}.'bar' );?
(rhs) (lhs). , k2 , %hash. , %hash . (%hash , .)
, ?
, . , .
my $cv1 = sub_result() * 5;
my $cv2 = "Number: $cv1";
my %h = (
k1 => $cv1,
k2 => $cv2,
k3 => "Numero: $cv1",
k4 => "Big $cv2",
k5 => "Round $cv2",
k6 => "Whole $cv2",
k7 => "-$cv1",
);
, , , , , , , . , , :
use Scalar::Util qw(reftype);
my @init = (
[ key1 => $value ],
[ key2 => \&make_a_value, 'key1' ],
[ key3 => \&another_way, 'key2' ],
);
my %h;
for my $spec ( @init ) {
my $key = shift @$spec;
my $value = shift @$spec;
my @args = @$spec;
if( reftype $value eq reftype sub {} ) {
$value = $value->( @h{ @args } );
}
$h{$key} = $value;
}
, , , , , . , . - . , .