Passing arrays to functions in Perl

前端 未结 3 1243
北荒
北荒 2021-02-19 04:03

I think I have misunderstood some aspects of argument passing to functions in Perl. What\'s the difference between func(\\@array) and func(@array)?

3条回答
  •  醉梦人生
    2021-02-19 04:30

    AFAIK, in both functions, arguments are passed by reference and in both functions we can change the elements of @array in the main program.

    "change the elements of", yes. However, in the func(@array) case, the sub has no means to make other changes to the array (truncating it, pushing, popping, slicing, passing a reference to something else, even undef'ing it).

    I would avoid using the term "passed by reference", since the mechanism is completely different than Perl's references. It is less overloaded :) to say that in the sub, @_'s elements start off aliased to the elements passed to the sub.

提交回复
热议问题