Search code examples
perlmoose

What exactly do this code snippet?


What exactly do this code snippet?

...
method go() {
    my %args;
    while ( ref( $_[0] ) eq 'HASH' ) {
        %args = ( %args, %{ shift(@_) } );
    }
    ....
}

Taken from a Moose based package...
While the first argument is a hashref, do what? (with the shifted out hashref?) I understand shift(@_) simply dont understand what's mean the

%hash = (%hash, %{$hashref});

Solution

  • While the first element of @_ is a reference to a hash, add the contents of that hash reference to the hash %args and remove the reference from @_. In other words, consolidate all of the initial hash arguments into a single hash.

    %a = (%a, %b) for some pair of hashes %a and %b is analogous to @a = (@a, @b) for some pair of arrays @a and @b, joining @a and @b into a single list and assigning the result back to @a. For arrays, there is of course a shorthand push @a, @b for just this purpose.