Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Any one-liners for set and get functions?

by sg (Pilgrim)
on Oct 05, 2007 at 07:24 UTC ( #642840=perlquestion: print w/replies, xml ) Need Help??

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
    There are modules on CPAN that can generatore them for you on the fly. A big one (and, at first sight, a rather complex one) is Class::Accessor. Will that do?

    There may be more lightweight ones available, but there's none that jumps at me right now.

    Yet, the principle isn't hard. Take a look at the source of WWW::Webrobot::Attributes for example: it's just one import routine, that generates a (combined getter/setter) sub for each attribute.

      A big one (and, at first sight, a rather complex one) is Class::Accessor.

      See Adam Kennedy's persuasive arguments as to why Object::Tiny is a better choice.

      • another intruder with the mooring in the heart of the Perl

      Another module that might be helpful is Moose, which provides a complete OO implementation
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://642840]
Approved by lima1
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2022-12-03 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?