What you have works for me - that is, this prints "foo bar":
use strict;
use warnings;
my @a = ("foo","bar");
my ($item1, $item2) = @a;
print "$item1 $item2\n";
I happen to be using Perl 5.13.4 on MacOS X 10.6.4, but I don't think that's a significant factor; I'd expect any Perl 5.x to accept it.
This code implements a 2D array:
use strict;
use warnings;
my @a;
$a[0][0] = "a00";
$a[0][1] = "a01";
$a[1][0] = "a10";
$a[1][1] = "a11";
my ($item1, $item2) = @a;
print "$item1 $item2\n";
The output is 'two ARRAY refs':
ARRAY(0x100803068) ARRAY(0x100826770)
Off-hand, I don't know of a way to expand the array of arrays into 4 separate values in a single operation. That isn't quite the same as "there is no way to do it".
And, implementing your foreach
loop:
foreach my $array (@a)
{
my($item1, $item2) = @$array;
print "$item1 $item2\n";
}
This prints:
a00 a01
a10 a11
This code is an example from the DBD::Informix distribution - last modified in 2002. It uses the fetchall_arrayref()
method which returns an array of array references, as mentioned in one of the comments to the question.
#!/usr/bin/perl -w
#
# DBD::Informix Example 5 - fetchall_arrayref
#
# @(#)$Id: x05fetchall_arrayref.pl,v 100.1 2002/02/08 22:50:10 jleffler Exp $
#
# Copyright 1998 Jonathan Leffler
# Copyright 2000 Informix Software Inc
# Copyright 2002 IBM
use DBI;
printf("DEMO1 Sample DBD::Informix Program running.\n");
printf("Variant 4: using fetchall_arrayref()\n");
my($dbh) = DBI->connect("DBI:Informix:stores7") or die;
my($sth) = $dbh->prepare(q%
SELECT fname, lname FROM customer WHERE lname < 'C'%) or die;
$sth->execute() or die;
my($ref) = $sth->fetchall_arrayref();
foreach $row (@$ref)
{
printf("%s %s\n", $$row[0], $$row[1]);
}
undef $sth;
$dbh->disconnect();
printf("\nDEMO1 Sample Program over.\n\n");
Were I writing it now (or updating it), there'd be use use strict;
as well as the -w
(equivalent to use warnings;
). It shows that back in 2002 (and ever since, AFAIK), you could write @$ref
happily enough. These days, I'd probably write the $$row[0]
references differently - as ${$row}[0]
.