bot403 has asked for the wisdom of the Perl Monks concerning the following question:
Monks. Following a previous example I've written the following perfectly WORKING code in my Item class constructor.
However, I'd like to take the next obvious step while furthering my perl knowledge and replace the last if block with a hash lookup. i.e replace the follwoing:
*$name = $setter; and *$name = $getter;
with something like:
*$name = $accessors->{$name}
or *$name = ${$accessors->{$name}}
package Item; sub new{ ...<make hash, bless, etc, etc>.... my $accessors = { team => "getter", status => "setter", }; for my $name (keys %{$accessors}){ # Defines read-write access to a DB column. my $setter = sub { my $s = shift; # Setting of value requested. if( @_ ){ ...<set value in database>... ...<other setter only code>... $self->{$name} = $value; } return $self->{$name}; }; my $getter = sub { my $s = shift; if( @_ ){ carp("Attempting to set value $_[0] for read-only var +$name!\n"); } return $self->{$name}; }; no strict 'refs'; if( $accessors->{$name} eq "setter" ){ *$name = $setter; }else{ *$name = $getter; } return $self; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: symbolic references in a hash lookup
by ikegami (Patriarch) on Jun 12, 2009 at 17:17 UTC | |
by bot403 (Beadle) on Jun 12, 2009 at 18:08 UTC | |
by ikegami (Patriarch) on Jun 12, 2009 at 18:19 UTC | |
by bot403 (Beadle) on Jun 12, 2009 at 18:35 UTC | |
by NetWallah (Canon) on Jun 12, 2009 at 18:19 UTC |