Commit 678336a1 authored by Leo Gordon's avatar Leo Gordon
Browse files

alternative substitution syntax #expr( #alpha#*#beta# )expr# and a test script

parent 7e60a6af
......@@ -65,6 +65,19 @@ use List::Util qw(first min max minstr maxstr reduce sum shuffle);
use Bio::EnsEMBL::Hive::Utils ('stringify', 'dir_revhash', 'go_figure_dbc'); # NB: dir_revhash() is used by some substituted expressions, do not remove!
=head2 new
Description: a trivial constructor, mostly for testing a Params object
=cut
sub new {
my $class = shift @_;
return bless {}, $class;
}
=head2 param_init
Description: First parses the parameters from all sources in the reverse precedence order (supply the lowest precedence hash first),
......@@ -236,7 +249,7 @@ sub param_substitute {
} else {
my $scalar_defined = 1;
$structure=~s/(?:#(.+?)#)/my $value = $self->_subst_one_hashpair($1, $overriding_hash); $scalar_defined &&= defined($value); $value/eg;
$structure=~s/(?:#(expr\(.+?\)expr|[\w:]+)#)/my $value = $self->_subst_one_hashpair($1, $overriding_hash); $scalar_defined &&= defined($value); $value/eg;
return $scalar_defined ? $structure : undef;
}
......@@ -316,7 +329,7 @@ sub _subst_one_hashpair {
} elsif($inside_hashes=~/^expr\((.*)\)expr$/) {
my $expression = $1;
$expression=~s/(?:\$(\w+))/stringify($self->_param_possibly_overridden($1, $overriding_hash))/eg;
$expression=~s{(?:\$(\w+)|#(\w+)#)}{stringify($self->_param_possibly_overridden($1 // $2, $overriding_hash))}eg;
$value = eval($expression);
}
......
#!/usr/bin/env perl
use strict;
use warnings;
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} });
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( 'adding indexed values: #expr( #age#->{Alice}+(max @{#gamma#}) )expr#' )."\n";
print $params->param_substitute( 'complex fold: #expr( join(", ", map { $ _.":".#age#->{$ _}} keys %{#age#}) )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