in reply to Perl Object Initilization
The easy way to do this is with Moose:
package Obj; use Moose; has 'a', is => 'ro', default => 10; package main; my $obj = Obj->new( a => 10 );
... though if you must write your own initializer, the easiest and clearest and probably most efficient way is:
sub initialize { my $self = shift; while (my ($key, $value) = splice @_, 0, 2)) { $self->{$key} = $value; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl Object Initialization
by eye (Chaplain) on Jun 11, 2011 at 14:35 UTC |