Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

resolved: Shorter than ternary?

by Spidy (Chaplain)
on Nov 15, 2007 at 21:04 UTC ( #651075=perlquestion: print w/replies, xml ) Need Help??
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?



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

    Something like this?

    use Data::Dumper; sub_with_defaults( { foo => 'real_foo' } ); sub sub_with_defaults { my %DEFAULTS = ( foo => 'default_foo', bar => 'default_bar', ); my %params = ( %DEFAULTS, %{shift @_} ); print Dumper( \%params ); } __END__ $VAR1 = { 'bar' => 'default_bar', 'foo' => 'real_foo' };
Re: resolved: Shorter than ternary?
by gamache (Friar) on Nov 15, 2007 at 21:58 UTC
    To allow defaults and/or enforce mandatory values in a hashref:
    my %args = ( key1 => 'specified 1', key3 => 'THIS SPACE FOR RENT' ); my $hr = { key1 => 'default 1', key2 => 'default 2', %args, key3 => 'enforced 3' }; ## now: ## $hr->{key1} == 'specified 1' ## $hr->{key2} == 'default 2' ## $hr->{key3} == 'enforced 3'
    In English: when building a hash, key => value pairs override the previous value for the given key. And tossing a %hash (or @array, if it looks like a hash) in the mix will just expand itself to a list of key, value, key, value...
Re: Shorter than ternary?
by jhourcle (Prior) on Nov 15, 2007 at 21:12 UTC

    Please define 'shorter'. If you just want less typing, use a subroutine:

    #nvl : a common pl/sql command sub nvl { defined ($_[0]) ? $_[0] : $_[1] }

    Or, you can use the fact that perl will use the second occurance of items in the assignment, but it might be less obvious, and they can override anything:

    my $defaults = { requires_login => 0, template_file => 'template.tmp', inner_template => '', %_ }
Re: Shorter than ternary?
by FunkyMonk (Canon) on Nov 15, 2007 at 21:12 UTC
    What about...
    my $defaults; $defaults->{$_} = $_{$_} ? $_{$_} : 0 for qw/ requires_login template_file /; $defaults->{inner_template} = '',

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://651075]
Approved by philcrow
[1nickt]: Ya gotta love the google. If you set accept=".txt" in a form input field where type="file", Chrome will allow you to select local files with extensions like .pl, .pm, .yaml, .ini etc. Nice!

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-07-22 18:19 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (340 votes). Check out past polls.