in reply to Maintaining strict refs while Binding a set of variables to @_ or dying if they are not defined

How do you like this approach:
sub someFunc { my @args = qw( client_number MM DD S ); # no commas here for (@args) { $_ = defined($_[0]) ? shift : die "$_ not defined"; } # hooray }
Hey, you could even use a pseudohash so you can use the names instead of indices:
sub someFunc { my $args = [ { client_number => 1, MM => 2, DD => 3, S => 4, }, ]; for (keys %$args) { my $idx = $args->[0]{$_} - 1; if (defined $_[$idx]) { $args->{$_} = $_[$idx], next } die "$_ not defined"; } # hooray }


japhy -- Perl and Regex Hacker
  • Comment on Re: Maintaining strict refs while Binding a set of variables to @_ or dying if they are not defined
  • Select or Download Code

Replies are listed 'Best First'.
Re: Re: Maintaining strict refs while Binding a set of variables to @_ or dying if they are not defined
by Fastolfe (Vicar) on Nov 30, 2000 at 01:38 UTC
    If you don't mind changing the calling conventions of your subroutine, go a step further and use named arguments in the form of a hash:
    sub someFunc { my %args = @_; my @arglist = qw{ client_number MM DD S }; foreach (@arglist) { die "$_ not given" unless exists $args{$_}; } # Now use $args{client_number} and $args{MM} }