Passing arrays to functions in Perl

前端 未结 3 1257
北荒
北荒 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:35

    func(\@array) passes a reference. func(@array) passes a list (of the elements in @array). As Keith pointed out, these elements are passed by reference. However, you can make a copy inside the sub in order to pass by value.

    What you are after is this:

    sub func {
        my @array = @_;
    }
    

    This will pass a copy of the arguments of func to @array, which is a local variable within the scope of the subroutine.

    Documentation here

提交回复
热议问题