<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">NAME

    assign - Destructuring Assignment Syntax for Perl

SYNOPSIS

    This code:

        use assign::0;
    
        my {$foo, bar =&gt; [ $first, @rest, $last ]} = $self-&gt;data;

    works the same as this code:

        my $_temp1 = $self-&gt;data;
        my $foo = $_temp1-&gt;{foo};
        my $_temp2 = $_temp1-&gt;{bar};
        my $first = $_temp2-&gt;[0];
        my @rest = @$_temp2[1..(@$_temp2-2)];
        my $last = $_temp2-&gt;[@$_temp2-1] if @$_temp2 &gt; 1;

DESCRIPTION

    This module enables a destructuring assignment syntax for Perl.

    Instead of assigning a value to a variable, you can now assign to a
    data structure (array-ref or hash-ref) that specifies how to unpack a
    data structure into all the variables you need.

    When you use this module you can put array-refs or hash-refs in places
    where you would put a variable that will be assigned to. The contents
    of these refs act as instructions for what variables to create and
    which parts of the target to take the assignment values from.

    There are many useful combinations, which are documented below. The
    destructuring refs can be nested, allowing you to extract all the
    values you want from a complex data structure in a single statement.

STATUS

    This module is very new and experimental. The hope is that this may one
    day become a pragma module or possibly even part of the Perl language's
    syntax.

    To preserve backwards compatability for early adopters, the module
    currently requires you to use assign::0;. When the module becomes
    stable and vetted, it will become simply use assign;.

USAGE

    Simply add a use assign::0; line to your program, and then you can use
    any of the assignment forms described below. You must use the module
    before the first line where you use one of the assignment forms.

    If you need to turn off assign after turning it on for some reason, you
    can use the line: no assign.

 DEBUGGING

    To see how the assign module turns the new style assignment forms into
    plain old Perl code in you program, you can use the assign::0-debug()&gt;
    method.

        require assign::0;
        print assign::0-&gt;debug(&lt;perl-file-name&gt;);
        print assign::0-&gt;debug(&lt;perl-code-string-as-scalar-ref&gt;);

    This will perform the assign transformations on the Perl code and print
    the result.

    You can do that as a perl one-liner like so:

        perl -e 'require assign::0; print assign::0-&gt;debug("program.pl")'

    Remember to use require assign::0;, not use assign::0;.

DESTRUCTURING FORMS

    The best way to explain this module is by example.

    All the forms below have been implemented and should work as described
    for this version of the module.

    Here we go...

 BASIC FORMS

      * my [$foo, $bar] = $array_ref;

      Define 2 my variables and assign the array-ref values in order.

      If there are less values on the RHS, the variables will be undefined.
      If there are more, they will be ignored.

      The RHS can be a variable or any expression as long as its value is
      an array-ref.

      * my [$foo, $bar] = (111, 222); ERROR

      If the LHS is an array-ref the RHS must also be an array-ref. If the
      LHS is a hash-ref the RHS must also be a hash-ref.

      * our [$foo, $bar] = $array_ref;

      Define our variables and assign the array-ref values.

      * local [$foo, $bar] = $array_ref;

      Define local variables and assign the array-ref values.

      * my $foo; our $bar; [$foo, $bar] = $array_ref;

      Assign the array-ref values to 2 pre-defined variables.

      * my {$foo, $bar} = {bar = 111, foo =&gt; 222};&gt;

      Define 2 my variables and use the variable name as the key to extract
      from the hash-ref on the RHS.

 EXTENDED ARRAY REF FORMS

      * my [ $foo, _, _, $bar ] = $array_ref;

      You can skip array values with the _ symbol.

      * my [ $foo, $_, $bar ] = $array_ref;

      Unpack into global $_, not a my variable. The $foo and $bar here are
      still my lexical vars.

      * my [ 7, $foo, 42, $bar ] = $array_ref;

      You can skip any number of array values by using a positive integer.

      * my [ $a, $b=42, $c="1\n2", $d=$a ] = $array_ref;

      You can define default values for variables. Currently the default
      must be a single token (string, number, variable). Also there must
      not be whitespace on either side of the =.

 TO DO

    There are many more forms that are intended to work, and many more
    cases we haven't thought of yet. Pretty much any place in Perl where a
    variable can be assigned to should also allow a structure to be
    assigned to.

    Here's the current list of things intended to be added soon:

        my [ $x1, @xs ] = $d;               # Set remaining into an array
        my [ $x1, @xs, $x2 ] $d;            # Set all but first and last into array
        my [ $first, [], $last ] = $d;      # Ignore middle
        my [ $x1, $x2, -25 ] = $d;          # Take -27 and -26
    
        my [ @a =&gt; @b =&gt; @c ] = $d;         # Evenly distribute values over multiple arrays
        my [ @a =&gt; @ =&gt; @ ] = $d;           # Take every third element (0, 3, 6, ...)
        my [ @a =&gt; @19 ] = $d;              # Take every 19th element
    
        my [ @a, @b, @c ] = $d;             # Split into thirds
        my [ @a, @, @ ] = $d;               # Get first third
    
        # Hash destructuring:
        my { $k1, $k2 } = $d;               # Unpack a hash ref
        my { $k1, $k2 } = %d;               # Unpack a hash
    
        my { k1 =&gt; $x, $k2 } = $d;          # Use a var name different than key
        my { k1 =&gt; $x=111, $k2=222 } = $d;  # Set default values
    
        my { $key =&gt; $val } = $d;           # Unpack a single pair hash
        my { @key =&gt; @val } = $d;           # Unpack all keys and values (unzip)
        my [ @key =&gt; @val ] = $d;           # Unpack all keys and values *sorted*
        my { @keys } = $d;                  # Key array of all keys
        my [ @keys ] = $d;                  # Key array of all keys *sorted* (RHS must be hashref)
        my { @keys =&gt; _ } = $d;             #   Same as above
        my [ @keys =&gt; _ ] = $d;             #   Same as above but *sorted*
        my { _ =&gt; @vals } = $d;             # Get array of all values
    
        my { 'a-key', 'b.key' } = $d;       # Short for { 'a-key' =&gt; $a_key, 'b.key' =&gt; $b_key }
        my { 'foo bar' } = $d;              # Short for { "foo bar" =&gt; $foo_bar }
    
        # Nested destructuring:
        my { k1 =&gt; { $k2, $k3 }} = $d;      # Unpack nested hash (no $k1)
        my { $k1 =&gt; { $k2, $k3 }} = $d;     # Unpack nested hash w/ $k1 set to inner hash ref
        my { k1 =&gt; [ $x1, $x2 ]} = $d;      # Unpack array ref nested in hash (no nesting depth limit)
    
        # Operator assignments:
        [ $a, $b ] //= $d;                  # Only assign undefined variables
        [ $a, $b ] .= $d;                   # Append string to every var
        [ $a, $b ] += $d;                   # Add number to every var
    
        # In for loops:
        for my { $k =&gt; [ $x1, $x2 ]} (@list) {  # Unpack each collection from a list
        for my { $k =&gt; $v } (%hash) { $d;   # Unpack each key/val pair from a hash
    
        # In signatures:
        sub foo( $a, {$k1, [ $x1, $x2 ]} ) { �� }
        sub foo({
            $name = "Fred",
            number =&gt; $num = 42,
        }) { �� }
    
        # Regex:
        my [ $match, $cap1, $cap2 ] = $str =~ /��/;
        my [ $match, $cap1, $cap2 ] = /��/;  # Match using $_
    
        # Inline list expressions:
        my [ $a, @l{reverse}, $y, $z ] = $d;
        my [ $a, @l{map ($_ + 1), grep ($_ &gt; 10)}, $z ] = $d;
        my [ $a, @{join '-'} =&gt; $s, $z ] = $d;

IMPLEMENTATION

    All of the new assignment forms introduced here would cause Perl syntax
    or runtime errors without using assign.pm.

    Currently this module is a working prototype that uses Filter::Simple
    (Perl source code filtering) and PPI.pm (to parse and restructure Perl
    code).

    Each destructuring form is removed and replaced with the appropriate
    Perl code to do the intended actions.

    When all the syntax forms have been implemented and fully tested and
    the module has become stable, it will be rewritten as an XS module.

    Note: Code transformations adjust the line numbers with #line &lt;num&gt;
    statements so that warnings and errors report line numbers that make
    sense.

PRIOR ART

    Destructuring assignment is available in many common languages. The
    assign module got many ideas from these.

      * CoffeeScript
      &lt;https://alchaplinsky.github.io/hard-rock-coffeescript/syntax/destructuring_assignment.html&gt;

      * JavaScript
      &lt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment&gt;

      * Python
      &lt;https://riptutorial.com/python/example/14981/destructuring-assignment&gt;

      * Clojure &lt;https://clojure.org/guides/destructuring&gt;

AUTHORS

      * Ingy d繹t Net &lt;&lt;ingy@ingy.net&gt;&gt;

COPYRIGHT AND LICENSE

    Copyright 2023 by Ingy d繹t Net

    This is free software, licensed under:

    The MIT (X11) License

</pre></body></html>