You can. It works either way.
use v5.10; use strict; use warnings; { package Local::Test; sub new1 { my $class = shift; bless {} => $class; } sub new2 { my $class = shift; my %hash = (); bless \%hash => $class; } sub does_it_work { say "yes, it works"; } } my $obj1 = Local::Test->new1; $obj1->does_it_work; my $obj2 = Local::Test->new2; $obj2->does_it_work;
And there's no rule that the blessed reference has to be a hashref; it can be any kind of reference. Hashrefs are usually the most convenient, but there are sometimes advantages to other structures. Arrayrefs tend to perform slightly better, though your object's internal structure is rarely the bottleneck in real life applications. If your object represents something file-like, then a blessed filehandle may be convenient. The URI package uses blessed references to scalar strings.
In reply to Re: Anonymous Hash in Constructor
by tobyink
in thread Anonymous Hash in Constructor
by puneet.keswani
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |