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;
+ }
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|