odrevet has asked for the wisdom of the Perl Monks concerning the following question:
The getters are very similar, is there some better way to have a more generic getter ? thankssub get_item_by_name { my ($this, $item_name) = @_; foreach my $item ( @{$this->{item}} ) { if ($item->{name} eq $item_name){ return $item; } } return undef; } sub get_user_by_id { my ($this, $user_id) = @_; foreach my $user( @{$this->{user}} ) { if ($user->{id} == $user_id){ return $user; } } return undef; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: efficient getters in module (class)
by roubi (Hermit) on Jul 25, 2009 at 15:34 UTC | |
|
Re: efficient getters in module (class)
by Anonymous Monk on Jul 25, 2009 at 15:03 UTC |