I have an optimization/minimum typing question. I wonder what the best and/or shortest method is for assigning default values to variables. For example, a typical subroutine call for me looks like:
use vars qw($VAR2); # global defaults and whatnot $VAR2 = 'thingamabob'; sub routine { my $input_ref = {@_}; my $var1 = $input_ref->{var1} || ''; my $var2 = $input_ref->{var2} || $VAR2; ...etc... }
This seems to work fairly well for me, however, it doesn't allow me create subroutine boolean flag called "do_this" that defaults to true, but the user can explicitly set false, eg:
sub routine { my $input_ref = {@_}; my $do_this = $input_ref->{do_this} || 1; }
If the user calles routine( do_this => 0 ), do_this will, obviously still be true. Now, I know how to code around this with:
my $do_this = 0; if ($input_ref->{do_this} eq '0') { $do_this = 0 } else { $do_this = 1 };
But that seems a little long winded. I was wondering if the Monks had any input (ha!) on this, or if my whole default assignment strategy is misguided.
Thanks,
Justin
In reply to Best method of assigning "default" values? by jqcoffey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |