in reply to Anonymous Hash in Constructor
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Anonymous Hash in Constructor
by Jim (Curate) on Oct 18, 2012 at 22:24 UTC | |
by tobyink (Canon) on Oct 19, 2012 at 05:50 UTC | |
by Jim (Curate) on Oct 19, 2012 at 19:20 UTC | |
by tobyink (Canon) on Oct 19, 2012 at 23:09 UTC | |
by Jim (Curate) on Oct 19, 2012 at 23:42 UTC |