Commit 2b2cc40a authored by Leo Gordon's avatar Leo Gordon
Browse files

bugfix: properly support evaluation of complex substituted expressions that yield a hashref

parent 355f5f19
......@@ -242,7 +242,7 @@ sub param_substitute {
return $structure;
} elsif($structure=~/^#([^#]*)#$/) { # if the given string is one complete substitution, we don't want to force the output into a string
} elsif($structure=~/^(?:#(expr\(.+?\)expr|[\w:]+)#)$/) { # if the given string is one complete substitution, we don't want to force the output into a string
return $self->_subst_one_hashpair($1, $overriding_hash);
......@@ -329,9 +329,12 @@ sub _subst_one_hashpair {
} elsif($inside_hashes=~/^expr\((.*)\)expr$/) {
my $expression = $1;
$expression=~s{(?:\$(\w+)|#(\w+)#)}{stringify($self->_param_possibly_overridden($1 // $2, $overriding_hash))}eg;
# FIXME: the following two lines will have to be switched to drop support for $old_substitution_syntax and stay with #new_substitution_syntax#
$expression=~s{(?:\$(\w+)|#(\w+)#)}{stringify($self->_param_possibly_overridden($1 // $2, $overriding_hash))}eg; # substitute-by-value (bulky, but supports old syntax)
# $expression=~s{(?:#(\w+)#)}{\$self->_param_possibly_overridden('$1', \$overriding_hash)}g; # substitute-by-call (no longer supports old syntax)
$value = eval($expression);
$value = eval "return $expression"; # NB: 'return' is needed to protect the hashrefs from being interpreted as scoping blocks
# warn "SOH: #$inside_hashes# becomes $expression and is then evaluated into ".stringify($value)."\n";
}
warn "ParamWarning: substituting an undefined value of #$inside_hashes#\n" unless(defined($value));
......
......@@ -7,13 +7,28 @@ use Bio::EnsEMBL::Hive::Params;
my $params = Bio::EnsEMBL::Hive::Params->new();
$params->param_init(1, { 'alpha' => 2, 'beta' => 5, 'gamma' => [10,20,33,15], 'delta' => '#expr( #alpha#*#beta# )expr#', 'age' => { 'Alice' => 17, 'Bob' => 20, 'Chloe' => 21} });
$params->param_init(1, {
'alpha' => 2,
'beta' => 5,
'delta' => '#expr( #alpha#*#beta# )expr#',
'gamma' => [10,20,33,15],
'gamma_prime' => '#expr( [ @{#gamma#} ] )expr#',
'age' => { 'Alice' => 17, 'Bob' => 20, 'Chloe' => 21},
'age_prime' => '#expr( { %{#age#} } )expr#',
});
print $params->param_substitute( "Substituting one scalar: #alpha# and another: #beta# and again one: #alpha# and the other: #beta# . Their product: #delta#\n" );
print $params->param_substitute( 'Old syntax needs single quotes or escaping the dollar. #expr( "One scalar: $alpha and another: $beta and again one: $alpha and another: $beta" )expr#' )."\n";
print $params->param_substitute( "This is csvq:gamma -> #csvq:gamma# and this is an expr()expr -> #expr( join(', #alpha#, ', sort \@{#gamma#}))expr#\n" );
print $params->param_substitute( "This is csvq:gamma -> #csvq:gamma# and this is an expr()expr -> #expr( join(', ', sort \@{#gamma#}))expr#\n" );
print $params->param_substitute( 'adding indexed values: #expr( #age#->{Alice}+(max @{#gamma#}) )expr#' )."\n";
print $params->param_substitute( 'complex fold: #expr( join(", ", map { $ _.":".#age#->{$ _}} keys %{#age#}) )expr#' )."\n";
print $params->param_substitute( 'joined gamma: #expr( join(", ", @{ #gamma# } ) )expr#'."\n" );
print $params->param_substitute( 'joined gamma_prime: #expr( join(", ", @{#gamma_prime#}) )expr#'."\n" );
print $params->param_substitute( 'complex fold of age: #expr( join("\t", map { $ _.":".#age#->{$ _}} keys %{#age#}) )expr#' )."\n";
print $params->param_substitute( 'complex fold of age_prime: #expr( join("\t", map { $ _.":".#age_prime#->{$ _}} keys %{#age_prime#}) )expr#' )."\n";
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment