You could but it should look like this with "standard/common" perl indentation and some error checking. Also if you are going to set_xxx you should get_xxx, not getxxx or getXxx to be conSistent:
set_serverid {
my $self = shift;
if (@_) {
$self->{SERVER} = shift;
}
else {
die "No argument passed to set_serverid!\n";
}
}
get_serverid {
my $self = shift;
$self->{SERVER};
}
shift syntax works fine for $self but often you might like to do a bit more error checking in your setters:
set_serverid {
my ($self,$id) = @_;
if (defined $id) {
die "Invalid value for id '$id' in set_serverid!\n"
unless $id > 0 and $id < 255;
$self->{SERVER} = $id;
}
else {
die "No argument passed to set_serverid!\n";
}
}
As noted I like to start off with very simple accessors like:
sub get_serverid { $_[0]->{SERVER} }
sub set_serverid { $_[0]->{SERVER} = $_[1] }
sub get_requestip { $_[0]->{REQIP} }
sub set_requestip { $_[0]->{REQIP} = $_[1] }
The thing I like about this is that you can see typos in the hash key names very easily. Unless you are doing formal error checking on the set side you don't really need more. |