Maybe you just want something like this;
package Foo;
sub new {
my $pkg = shift;
my %self = @_;
my $self = bless \%self, $pkg;
# do stuff to $self (now a __PACKAGE__)
return $self;
}
1;
Then,
use Data::Dumper ();
my $foo = Foo->new(a => 1, b => 2, c => [qw/apple banana grape/]);
print Data::Dumper::Dumper(\$foo)
Output:
$VAR1 = \bless( {
'b' => 2,
'c' => [
'apple',
'banana',
'grape'
],
'a' => 1
}, 'Foo' );
If you want the basis for a
Foo to be an array ref, then,
package Foo;
sub new {
my $pkg = shift;
my @self = @_;
my $self = bless \@self, $pkg;
# do stuff to $self (now a __PACKAGE__)
return $self;
}
1;
Output:
$VAR1 = \bless( [
'a',
1,
'b',
2,
'c',
[
'apple',
'banana',
'grape'
]
], 'Foo' );
Note,
=> is taken as a
comma when an array is on the LHS of the assignment (
=). Hashes can be treated as even-sized arrays - or arrays of
tuples (i.e.,
key => val).