stevieb has asked for the wisdom of the Perl Monks concerning the following question:
Reviewing some of my code, and I'm wondering if and how the Monks would write these rw accessors to make them more concise (ie. more compact, without using external modules). Bonus points for a solution that uses a single routine to cover both, but is easy to an experienced Perl dev to look at in six months and immediately identify without any question what's happening.
sub ip { my ($self, $ip) = @_; return $self->{ip} if $self->{ip}; if (! $ip && $self->{conf}{ip}){ $ip = $self->{conf}{ip}; } $ip = '0.0.0.0' if ! $ip; $self->{ip} = $ip; } sub port { my ($self, $port) = @_; return $self->{port} if $self->{port}; if (! $port && $self->{conf}{port}){ $port = $self->{conf}{port}; } $port = '7800' if ! defined $port; $self->{port} = $port; }
{conf} is simply an element from an ini-style config file, which may not be available, and that would make the statement untrue.
I'm taking off camping in the mountains in the AM, so I'll review any replies when I return on Monday.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: More concise way to write these accessors?
by BrowserUk (Patriarch) on Jun 25, 2016 at 04:10 UTC | |
|
Re: More concise way to write these accessors?
by $h4X4_|=73}{ (Monk) on Jun 25, 2016 at 08:12 UTC |