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.


In reply to Re^2: Dhcpclient module by tachyon-II
in thread Dhcpclient module by drip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.