Spidy has asked for the wisdom of the Perl Monks concerning the following question:
Greetings, fellow monks.
I have a module I'm working on that accepts a hash of arguments to one of its functions. If no hash is supplied, it fills in the arguments itself. This is the code I am using:
my $defaults = { requires_login => $_{'requires_login'} ? $_{'requires_ +login'} : 0, template_file => $_{'template_file'} ? $_{'template_fi +le'} : 'template.tmpl', inner_template => '', };
However, there are a lot of defaults I need to do this for. Is there any way to write this in a shorter form and have it work?
Thanks,
Spidy
Edit: I ended up just hacking a piece of HTML::Template to suit my needs:
sub loadOptions { my $argsref = shift; my $options = shift; for(my $i = 0; $i < @{$argsref};$i+=2) { $options->{lc(${$argsref}[$i])} = ${$argsref}[($i+1)]; } return $options; }
And then it's called using:
$defaults = loadOptions([@_],$defaults);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Shorter than ternary?
by kyle (Abbot) on Nov 15, 2007 at 21:12 UTC | |
|
Re: resolved: Shorter than ternary?
by gamache (Friar) on Nov 15, 2007 at 21:58 UTC | |
|
Re: Shorter than ternary?
by jhourcle (Prior) on Nov 15, 2007 at 21:12 UTC | |
|
Re: Shorter than ternary?
by FunkyMonk (Bishop) on Nov 15, 2007 at 21:12 UTC |