Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Force access a Hash ref as Array ref

by perlfan (Vicar)
on Jul 20, 2021 at 22:17 UTC ( #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? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-01-23 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (63 votes). Check out past polls.

    Notices?