develooper Front page | perl.perl5.porters | Postings from June 2008

Re: [perl #56150] return return

Thread Previous | Thread Next
From:
Mark Mielke
Date:
June 23, 2008 07:18
Subject:
Re: [perl #56150] return return
Message ID:
485FB0B0.6090405@mark.mielke.cc
H.Merijn Brand wrote:
>> sub NAME
>> {
>>     ... lots of code ...
>>
>>     undef;
>> }
>>     
>
> What if the function can return a list or a scalar? "return" handles
> that automatically, undef does not;
>   

Hehe - good question. My answer is that it tends not to happen for my 
style of programming. That is, a function that returns a list would take 
the form:

sub NAME
{
    my @results;

    ... do whatever to @results ...

    @results;
}

Now, I do recall it happening once in the past where I found the need to 
return from @results from inside a conditional - and I think I decided 
on "return ();" as I found "return;" to not communicate "return an empty 
list" as explicitly as I wanted. It was an imperfect situation. :-(

> BTW I agree with both worlds. In some cases it should suffice to use
> the last value, in some cases one should use return for clarity
>   

Agree. I was playing devil's advocate - not intending to say "never ever 
use return!". I only challenged the claim that one should "always use 
return!". :-) It came across as dogma like "never use goto!". :-)

Cheers,
mark

-- 
Mark Mielke <mark@mielke.cc>


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About