in reply to Re: RFC: Destructuring Assignment (aka Unpacking aka Type Patterns) in pure Perl
in thread RFC: Destructuring Assignment (aka Unpacking aka Type Patterns) in pure Perl

> which pulls the values at the named keys out of arg_map into vars named after the respective keys

That's kind of a hash slice but in a DRY way to avoid typing and typos???

my ($foo, $bar) = @arg_map{qw/foo bar/} ?

Sure I can, but I'd need another command and would have a dependency to PadWalker. (Probably)

Something like set_key(my($foo, $bar)) would just return° the needed pairs from the solution in the OP

Edit

°) That is the equivalent to { foo => set my $foo , bar => set my $bar }

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^3: RFC: Destructuring Assignment (aka Unpacking aka Type Patterns) in pure Perl
by Fletch (Bishop) on Jul 04, 2020 at 03:52 UTC

    Sort of like your slice, but you also can provide default values if things are missing. E.g. if arg_map didn't have anything at the key :foo the variable foo would get be the string "foo missing".

    (let [{:keys [foo bar] :or {foo "foo missing"}} arg_map] ;; ... )

    You'd prossibly need something like my $foo = exists $arg_map{foo} ? $arg_map{foo} : "foo missing" and you'd need to do each variable separately. (I've not messed with clojure in long enough that I'm probably explaining this poorly, to boot . . .)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Individual defaults are no problem.

      set my $var= DEFAULT

      does it already.

      If there is no prior value, it will be undef because of my .

      If you need a dedicated Missing different from undef I could return an error object.

      Implementation is no problem, I'd need to think about the best API.

      There might also be the case of reused variables - like in a loop - were you skipped the my, but don't want to default to the prior value but undef.

      set $var

      Again only a question of designing a intuitive API/Syntax and testing use cases.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      °) my defaults to undef