theantler has asked for the wisdom of the Perl Monks concerning the following question:

Hello folks, we can make an array in straightforward ways like this:
my @array = (1, 2, 3); just an array my $ref = \@array; get the ref to the array my $ref = [1, 2, 3]; make an anonymous array and get the ref
But ... what does this do:
$xxx = [ @array ]; huh??? Hey I think I found out: we mke a copy of th +e array in an anonymous and get the ref to it.
and ..
@$array = (1, 2, 3); what?!
</code>

Replies are listed 'Best First'.
Re: Four or five ways we can make or break an array?
by BrowserUk (Patriarch) on Mar 20, 2010 at 12:45 UTC
    @$array = (1, 2, 3);  what?!

    Treat $array as an array reference, and assign the list to that array.

    If $array already points to an array, overwrite that array with the list.

    If $array is undef, create (autovivify) an array and assign a reference to it to $array.

    If $array currently has any value other than undef or an array reference, and strict is in force, die.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      OK .. I didnt know it would be a bad thing mention .. Sorry!
Re: Four or five ways we can make or break an array?
by ikegami (Patriarch) on Mar 20, 2010 at 17:00 UTC
    my @array; my $ref = \@array; @$ref = (1,2,3);
    is the same as
    my @array; @array = (1,2,3);
Re: Four or five ways we can make or break an array?
by CountZero (Bishop) on Mar 20, 2010 at 17:20 UTC
    And yet another way to make an array:
    my @array = qw/an array of six single words/;

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Four or five ways we can make or break an array?
by biohisham (Priest) on Mar 20, 2010 at 17:08 UTC
    You might find References quick reference interesting.
    C:\Documents and Settings\user>perl - @array = (1,2,3,4); my $x_ref = [@array]; print "$x_ref\n"; print "@$x_ref\n"; __END__ ARRAY(0x282a374) 1 2 3 4


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.