elTriberium has asked for the wisdom of the Perl Monks concerning the following question:
Hi all Monks,
Below I pasted a small code fragment. My plan is to allow each subroutine to set the object variables. If an argument is provided then it should overwrite the current object value. If an argument is not provided then it should use the current object variable value. It should die when no argument is provided for a variable and none is set yet. I'm basically only looking for a best practice here, as my solution looks too verbose.
If you think this in general is not a good design choice please also let me know why.
package Remote; use Moose; has 'resource' => (is => 'rw', isa => 'Str'); has 'username' => (is => 'rw', isa => 'Str'); sub remote_run { my ($self, %args) = @_; my $resource = $args{'resource'} || $self->resource; my $username = $args{'username'} || $self->username; die "Missing resource variable" if (!defined($resource)); die "Missing username variable" if (!defined($username)); $self->resource($resource); # In case this is a new value. $self->username($username); # Here comes the actual code ... }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How would you simplify this code?
by chromatic (Archbishop) on Apr 07, 2011 at 05:32 UTC | |
by elTriberium (Friar) on Apr 07, 2011 at 05:46 UTC | |
by chromatic (Archbishop) on Apr 07, 2011 at 17:26 UTC | |
Re: How would you simplify this code?
by ikegami (Patriarch) on Apr 07, 2011 at 06:28 UTC | |
Re: How would you simplify this code?
by NetWallah (Canon) on Apr 07, 2011 at 04:20 UTC | |
by elTriberium (Friar) on Apr 07, 2011 at 05:48 UTC |
Back to
Seekers of Perl Wisdom