in reply to Very very small style question
Well, I'd probably help my user by catching some coding mistakes rather than letting them cause strange behavior that they'd have to spend much more time debugging:
But I don't like the magic numbers there so I might avoid them at the cost of some duplication but with even better error messages:sub method { my $self= shift(@_); croak 'Usage: $oldVal= $obj->method( $idx [, $newVal ] )',$/ unless 1 <= @_ && @_ <= 2; my $i= shift(@_); my $old= $self->{B}[$i]; $self->{B}[$i]= shift(@_) if @_; return $old; }
Then you can remove some duplication with:sub method { my $self= shift(@_); croak 'Missing $idx; usage: $oldVal= $obj->method( $idx [, $newVal + ] )',$/ unless @_; my $i= shift(@_); my $old= $self->{B}[$i]; $self->{B}[$i]= shift(@_) if @_; croak 'Too many args; usage: $oldVal= $obj->method( $idx [, $newVa +l ] )',$/ if @_; return $old; }
- tye (but my friends call me "Tye"){ my $usage; BEGIN { $usage= '$oldVal= $obj->method( $idx [, $newVal ] )' } sub method { my $self= shift(@_); croak "Missing \$idx; usage: $usage\n" unless @_; my $i= shift(@_); my $old= $self->{B}[$i]; $self->{B}[$i]= shift(@_) if @_; croak "Too many args; usage: $usage\n" if @_; return $old; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: (tye)Re: Very very small style question
by MeowChow (Vicar) on Dec 19, 2000 at 00:06 UTC | |
by tye (Sage) on Dec 19, 2000 at 00:28 UTC |