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

In reply to Re: Force access a Hash ref as Array ref by perlfan
in thread Force access a Hash ref as Array ref by exilepanda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.