Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How would you simplify this code?

by elTriberium (Friar)
on Apr 07, 2011 at 04:01 UTC ( #897927=perlquestion: print w/replies, xml ) Need Help??

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

    If this were my code, I'd make both attributes required so that it's impossible (or at least unlikely, if you follow the API documentation) to create a Remote object without a valid resource and username.

      That's a good comment, however what if these 2 (or other) attributes are only required in this subroutine and not in all subroutines of the class?

        Some classes work out that way. It's not necessarily a design smell.

Re: How would you simplify this code?
by NetWallah (Canon) on Apr 07, 2011 at 04:20 UTC
    $args($_) ? $self->$_($args{$_}) : $self->$_ or die "Missing variable + $_" for qw|resource username|;
    Just use "$self-><Varname>" instead of the lexically defined var names for the rest of the sub.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      Cool, didn't know that this would work. I like the idea but it looks very "line noise"-ish.
Re: How would you simplify this code?
by ikegami (Patriarch) on Apr 07, 2011 at 06:28 UTC
    sub remote_run { my ($self, %args) = @_; $self->resource( $args{resource} ) if $args{resource}; $self->username( $args{username} ) if $args{username}; my $resource = $self->resource or die("'resource' must be provided"); my $username = $self->username or die("'username' must be provided"); # Here comes the actual code ... }

    Sure, you could use a loop, but I think it would reduce readability even though it would remove some redundancy.

    This usage is rather odd, which is why there's no clear winning way of doing it. I'd use args or attributes, not this mixture.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2023-05-28 02:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found