in reply to Passing by Named Parameter

I might do something like this:

sub mysub { %params = %{ +shift }; my %known_options = ( 'this' => 1, 'that' => 2 ); PARSE: foreach my $param ( keys %params ) { exists $known_options{$param} && do { #something; next PARSE; }; die "Usage: I only know how to 'this' and 'that'\n"; } # Rest of sub here... }

Just One Way To Do It.

The values assigned to keys in %known_options are just numeric for this example, but could be coderefs, subrefs, flag values, or whatever you want.

Update: Better late than never, fixed %params = %{ +shift }; syntax.


Dave

Replies are listed 'Best First'.
I've never seen =that= before!
by John M. Dlugosz (Monsignor) on Jan 27, 2004 at 15:41 UTC
    %params = %{ shift };
    When I first read it, I thought it implied that shift in list context returns them all and empties the list; that is, gives the same value as %params= @_; but also empties out @_.

    After reading Abagail's explaination, I see that he's passing a hashref as the first parameter, rather than passing names/values as individual parameters.

      And you don't want to. It means the same as
      %params = %shift;
      What you might want to see instead is:
      %params = %{+shift};

      Abigail