Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Force access a Hash ref as Array ref

by perlfan (Vicar)
on Jul 20, 2021 at 22:17 UTC ( [id://11135239]=note: print w/replies, xml ) Need Help??


in reply to Force access a Hash ref as Array ref

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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135239]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found