Are Perl subroutines call-by-reference or call-by-value?

后端 未结 5 1914
太阳男子
太阳男子 2020-12-08 00:59

I\'m trying to figure out Perl subroutines and how they work. From perlsub I understand that subroutines are call-by-reference and that an assignment (like my(@copy) =

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 01:20

    (Note that use warnings is even more important than use strict.)

    @_ itself isn't a reference to anything, it is an array (really, just a view of the stack, though if you do something like take a reference to it, it morphs into a real array) whose elements each are an alias to a passed parameter. And those passed parameters are the individual scalars passed; there is no concept of passing an array or hash (though you can pass a reference to one).

    So shifts, splices, additional elements added, etc. to @_ don't affect anything passed, though they may change the index of or remove from the array one of the original aliases.

    So where you call change(@a), this puts two aliases on the stack, one to $a[0] and one to $a[1]. change(%a) is more complicated; %a flattens out into an alternating list of keys and values, where the values are the actual hash values and modifying them modifies what's stored in the hash, but where the keys are merely copies, no longer associated with the hash.

提交回复
热议问题