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

I can't easily picture how you'd hammer it into the language without needing parser changes but an Haskell/ML/Erlang destructuring where things are pulled apart based on types and/or guard conditions and the like could be very interesting.

You might also look at clojure for more inspiration that's a bit closer to the ES6 stuff (I believe).

E.g. where you can have something like [{:keys [foo bar]} arg_map] which pulls the values at the named keys out of arg_map into vars named after the respective keys.

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

Replies are listed 'Best First'.
Re^2: RFC: Destructuring Assignment (aka Unpacking aka Type Patterns) in pure Perl
by LanX (Saint) on Jul 03, 2020 at 20:57 UTC
    > 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

      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