in reply to Re: these aren't locals, but they seem to act like it
in thread these aren't locals, but they seem to act like it

I love your solution:

my %args; if (ref $_[0] eq "HASH") { %args = %{$_[0]}; } else { @args{qw(flags img name big gallery page caption num)} = @_; }

But one of the things I left out so as to keep the code concise was the use of default values to apply to the locals in the even the caller didn't pass them in. This is why I used the looping mechanism that I did. Rather than specifying each line precisely, which worked, I wrote a loop for brevity and attempted elegance. The solution above is what the doctor (or grandfather) ordered. So, let's revisit my other original objective again and replace what i took out:

$defaults = { flags => 0x8001, img => "/path/file.jpg", ... }; for (qw( flags img name big gallery page caption num ) ) { $$_ = $opts->{$_} || $defaults->{$_}; }

We know why this doesn't work now, but how can I apply defaults from a default hash for those values not passed in using the new method (to me) shown above?

Replies are listed 'Best First'.
Re^3: these aren't locals, but they seem to act like it
by mreece (Friar) on Mar 12, 2007 at 16:30 UTC
    consider:
    my %args = ( flags => 0x8001, img => "/path/file.jpg", ... ); if (ref $_[0] eq 'HASH') { @args{keys %{ $_[0] }} = values %{ $_[0] }; } else { for (qw( flags img name big gallery page caption num )) { $args{$_} = shift if @_; } }
Re^3: these aren't locals, but they seem to act like it
by sgifford (Prior) on Mar 12, 2007 at 16:43 UTC
    If you want the defaults to apply whether the parameters are passed by name or not, after you have initialized parameters from the values passed in to your sub you could simply say something like this (untested):
    foreach my $k (keys %args) { if (!defined($args{$k})) { $args{$k} = $defaults->{$k}; } }