sg has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
The following has nice one-liners for assigning parameters during initialization and for destroying parameters during destruction. However, I could not come up with any nice way to implement the tedious set and get sub-routines -- what's a nice way to implement them?
package ThePackage; use diagnostics; use warnings; use strict; use Scalar::Util qw(refaddr); use Carp; $Carp::Verbose = 1; { my ( %parameter_a_of, %p_b_of, %param_c_of, %parameter_d_of, ) = (); my %init_aid = ( paramater_a => \%parameter_a_of, p_b => \%p_b_of, paramater_c => \%param_c_of, paramater_d => \%parameter_d_of, ); sub new { my($class, $parameter_a, %h) = @_; my $new_object = bless \do{my $anon_scalar}, $class; $paramter_a_of{refaddr $new_object } = $parameter_a; defined $h{$_} and ${$init_aid{$_}}{refaddr $new_object} = $h{$_} for (keys %init_aid); return $new_object; } sub DESTROY { my ($self) = @_; delete ${$init_aid{$_}}{refaddr $self } for (keys %init_aid); return; } sub get_parameter_a { return $parameter_a_of{refaddr $_[0]} } sub get_p_b { return $p_b_of {refaddr $_[0]} } sub get_param_c { return $param_c_of {refaddr $_[0]} } sub get_parameter_d { return $parameter_d_of{refaddr $_[0]} } sub set_parameter_a{ $parameter_a_of{refaddr $_[0]} = $_[1]; return; + } sub set_p_b { $p_b_of {refaddr $_[0]} = $_[1]; return; + } sub set_param_c { $param_c_of {refaddr $_[0]} = $_[1]; return; + } sub set_parameter_d{ $parameter_d_of{refaddr $_[0]} = $_[1]; return; + } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Any one-liners for set and get functions?
by bart (Canon) on Oct 05, 2007 at 08:13 UTC | |
by grinder (Bishop) on Oct 05, 2007 at 08:59 UTC | |
by moritz (Cardinal) on Oct 05, 2007 at 09:14 UTC | |
Re: Any one-liners for set and get functions?
by jeanluca (Deacon) on Oct 05, 2007 at 08:06 UTC | |
Re: Any one-liners for set and get functions?
by FunkyMonk (Chancellor) on Oct 05, 2007 at 09:57 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Seekers of Perl Wisdom