Get hash keys/values on same line as the function call

前端 未结 4 1524
难免孤独
难免孤独 2021-01-15 10:21

Here is the code to reproduce the problem:

sub hello { return (h => 1, n => 1); }
print join \", \", values hello();

I get the error:

4条回答
  •  醉话见心
    2021-01-15 10:53

    I don't believe this is possible because Perl is not strongly enough typed to know what subroutines return.

    As far as Perl is concerned, all subroutines simply return LISTs (or a single SCALAR). LISTs can have certain operations applied to them (indexing, slicing, etc.), but nothing that requires an ARRAY variable (like push, pop, shift) or a HASH variable (including keys, values, delete, exists).

    Hash assignment takes in a LIST as a parameter (which your function returns), and creates an associative hash with every odd element serving as a key to the next even element. Only after this assignment can it be called a HASH in Perl's grammar, and therefore only then will it be usable in the values function.

提交回复
热议问题