Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: "Fields" for "Objects"

by vek (Prior)
on Jun 10, 2009 at 22:52 UTC ( #770468=note: print w/replies, xml ) Need Help??

in reply to "Fields" for "Objects"

I usually write getter/setter combos:
sub accountnumber { my ($self, $num) = @_; if (defined($num)) { $self->{account_num} = $num; return $self; } else { return $self->{account_num}; } }
But even that's a pain in the arse too. So, Class::Accessor for the win.
use base qw(Class::Accessor); __PACKAGE__->mk_accessors(qw(accountnumber));
-- vek --

Replies are listed 'Best First'.
Re^2: "Fields" for "Objects"
by whakka (Hermit) on Jun 11, 2009 at 00:09 UTC
    It's interesting, this is how I write them too. But I noticed lodin's node, which contains an important but subtle difference; by doing this:
    sub field { my $self = shift; $self->{field} = $_[0] if @_; ... }
    instead of this:
    sub field { my ($self,$val) = @_; $self->{field} = $val if defined $val; ... }
    you don't run into issues where $self->field(undef) would silently return the old value without changing it, which isn't what you might expect. But because I've seen the second form elsewhere and it's technically better practice to assign @_ to variables I just stuck with it, but clearly the first way is better.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://770468]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2022-01-23 10:26 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (63 votes). Check out past polls.