doran has asked for the wisdom of the Perl Monks concerning the following question:
Is it always okay to return a value from a routine that is sometimes called in a void context?
For example, take this classic from the cookbook:
Is there a reason it would be better written as:sub name { my $self = shift; if (@_) { $self->{NAME} = shift } return $self->{NAME}; }
Of course there may be specific cases where I'd want to avoid returning a value in a void context, but this question is under the "general rule" category. Is there a problem with returning a value in a void context?sub name { my $self = shift; if (@_) { $self->{NAME} = shift } return $self->{NAME} if defined wantarray; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: OK to always return a value?
by davido (Cardinal) on May 09, 2005 at 02:04 UTC | |
by doran (Deacon) on May 09, 2005 at 02:11 UTC | |
|
Re: OK to always return a value?
by Zaxo (Archbishop) on May 09, 2005 at 01:54 UTC | |
|
Re: OK to always return a value?
by merlyn (Sage) on May 09, 2005 at 02:16 UTC | |
by doran (Deacon) on May 09, 2005 at 03:05 UTC |