Perl: array reference versus anonymous array

后端 未结 3 1955
无人及你
无人及你 2021-01-12 06:08

This may be a silly question... The following code outputs the contents of @arrayref and @arraycont respectively. Note the difference between them

3条回答
  •  无人共我
    2021-01-12 06:46

    This creates a shallow copy of the array:

    $arraycont[0] = [@array];

    Whereas this just creates a reference to it:

    $arrayref[0] = \@array;

    Since you later modify the array:

    @array = qw(5 6 7 8);

    arrayref still points to the same array location in memory, and so when dereferenced in the print statements it prints the current array values 5 6 7 8.

提交回复
热议问题