Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Very very small style question

by blogan (Monk)
on Dec 18, 2000 at 05:37 UTC ( [id://47166]=note: print w/replies, xml ) Need Help??


in reply to Very very small style question

I prefer either one of these two:
sub method { my ($self, $i, $new) = @_; my $old = $self->{B}[$i]; #make sure we have correct number of parameters $self->{B}[$i] = $new if @_ > 2; return $old; }
or
sub method { my ($self, $i, $new) = @_; my $old = $self->{B}[$i]; #make sure we have correct number of parameters $self->{B}[$i] = $new if @_ >= 3; return $old; }
Couldn't you do an (if defined $new) also?

Replies are listed 'Best First'.
Re: Re: Very very small style question
by chipmunk (Parson) on Dec 18, 2000 at 07:15 UTC
    The one reason for checking the size of @_, rather than whether $_[2] is defined, is that you may actually want to allow someone to call the method with undef as an argument.
Re: Very very small style question
by Dominus (Parson) on Dec 18, 2000 at 09:20 UTC
    Says blogan:
    > Couldn't you do an (if defined $new) also?
    No. chipmunk is exactly right here; the function needs to be able to set members of the B array to undefined. In fact, an earlier version of the code did use ...if defined $new and had a terrible bug that led to an infinite loop, because items weren't being set to undef when they needed to be.

    Thanks for the suggestion, though.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found