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

I thought that \( list ) and [ list ] did the same thing but apparently not,
Can someone explain why the first structure below works while the second doesnt?
@ary = ('item1:value1:text1', 'item2:value2:text2'); for (@ary) { push @flibble, [ split /:/, $_ ]; } for (@ary) { push @flibble, \(split /:/, $_ ); } # then for (@flibble) { print "row : @{ $_ } \n"; # to test }

Thanks.

Replies are listed 'Best First'.
RE: creating array refs
by Boogman (Scribe) on Aug 21, 2000 at 22:32 UTC
    \( element1, element2, ... ) takes a reference to each element in the list and returns that list (i.e. ( \element1, \element2, \... ) whereas [ element1, element2, ... ] returns a reference to an array containing those elements.

    When you push the results of taking a refererence to a list into @fibble, you add a reference to each element in the list individually, as opposed to adding a reference to an array containing those elements as you do using the anonymous array composers [].
Re: creating array refs
by btrott (Parson) on Aug 21, 2000 at 22:19 UTC
    \(list) isn't valid syntax. And that's what you're doing with split, because split returns a list. You can't take a reference to a list; you can only use a list to build an anonymous array ref.

    \@array is valid syntax, because you can take a reference to an array.

    The difference is, LIST creates a new anonymous array reference and fills it with LIST; \@ARRAY takes a reference to an existing array.