Click here to get back home

Use of "return" in place of "last" (newbie question)?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Use of "return" in place of "last" (newbie question)? Michael 03-09-2008
Posted by Michael on March 9, 2008, 10:34 am
Please log in for more thread options
I am new to perl, and I am wondering whether it is a bad thing to jump out
of a foreach loop with return. I have a function which searches for a
value in one of several arrays. If the value is found in any of them,
then the whole function may as well terminate. At the moment I am setting
a flag when the value is found, which stops execution of the later loops.
Would it be safe / stylistically sound to do away with the flag by using
"return" rather than "last". Example code follows.

TIA

sub example{

my $found;
$found = 0;

foreach $item (@list)
        {
         if ($value eq $item)
                {
                print ("found $value\n");
                $found = 1;
                last;
                }        
        }

... several other foreach loops follow in which the only thing which
differs is the name of the array which is "@list" above, and the test for
the flag.

return
}

Posted by Tad J McClellan on March 9, 2008, 11:16 am
Please log in for more thread options

> I am new to perl, and I am wondering whether it is a bad thing to jump out
> of a foreach loop with return.


Yes it is a bad thing, because it will not work. :-)


> I have a function which searches for a
> value in one of several arrays. If the value is found in any of them,
> then the whole function may as well terminate.


Oh.

Using a return() to return from a subroutine is perfectly fine
(even if the return() is within a loop).


> Would it be safe / stylistically sound to do away with the flag by using
> "return" rather than "last".


Yes.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Posted by Michael on March 9, 2008, 12:58 pm
Please log in for more thread options
On Sun, 09 Mar 2008 10:16:08 -0500, Tad J McClellan wrote:
<snip>
>
> Using a return() to return from a subroutine is perfectly fine (even if
> the return() is within a loop).
>
>> Would it be safe / stylistically sound to do away with the flag by
>> using "return" rather than "last".
>
> Yes.

Many thanks for the prompt and helpful response Tad.

/M


Posted by Jürgen Exner on March 9, 2008, 1:14 pm
Please log in for more thread options
>I am new to perl, and I am wondering whether it is a bad thing to jump out
>of a foreach loop with return.

Technically it doesn't make a difference.

Stylistically it is a matter of which programming philosophy you are
following. For GOTOists it is standard operating procedure, for functional
programmers it is a sacrilege. Others are somewhere in between.

jue

Posted by Joost Diepenmaat on March 9, 2008, 1:23 pm
Please log in for more thread options

>>I am new to perl, and I am wondering whether it is a bad thing to jump out
>>of a foreach loop with return.
>
> Technically it doesn't make a difference.
>
> Stylistically it is a matter of which programming philosophy you are
> following. For GOTOists it is standard operating procedure, for functional
> programmers it is a sacrilege. Others are somewhere in between.

Functional programmers don't use foreach either :-)

Personally, I think using a return statement generally leads to less
convoluted, clearer code.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Similar ThreadsPosted
Perl newbie - getting "system()" to return January 31, 2005, 4:05 am
MS Perl question -- how to use hacked script to work correctly(was Question on loops and return values or sumpin) December 8, 2004, 12:59 pm
Question on loops and return values December 3, 2004, 3:02 pm
multiple return regex question December 11, 2007, 4:04 pm
Return Codes in Perl: Question on implementing good patterns April 27, 2005, 9:32 am
newbie question September 12, 2004, 11:06 am
A newbie question October 14, 2004, 7:51 pm
Newbie Question November 6, 2004, 6:10 am
newbie-ish question November 18, 2004, 3:53 am
newbie question. Please help! November 28, 2004, 7:53 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap