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);
In reply to resolved: Shorter than ternary? by Spidy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |