What's the best Perl practice for returning hashes from functions?

后端 未结 7 1951
走了就别回头了
走了就别回头了 2021-02-04 07:31

I am mulling over a best practice for passing hash references for return data to/from functions.

On the one hand, it seems intuitive to pass only input values to a fun

7条回答
  •  心在旅途
    2021-02-04 08:30

    Just return the reference. There is no need to dereference the whole hash like you are doing in your examples:

    my $result = some_function_that_returns_a_hashref;
    say "Foo is ", $result->{foo};
    say $_, " => ", $result->{$_} for keys %$result;
    

    etc.

    I have never seen anyone pass in empty references to hold the result. This is Perl, not C.

提交回复
热议问题