Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl loops should use break, not last

Reply
Thread Tools

Perl loops should use break, not last

 
 
Jeremy Morton
Guest
Posts: n/a
 
      01-29-2005
Probably been mentioned before but I fail to see why Perl changed the
'break' keyword to 'last', in loops. Bear with me on this - it seems
semantically more accurate to say 'break' - you're immediately breaking out
of the loop. 'last' makes it sound like the current loop will be the last,
but not that the execution should be stopped immediately, whereas break
makes it sound like the latter.

Where can I propose that this be changed, or break aliased to last, for Perl
6?


--
Best regards,
Jeremy Morton (Jez)


 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      01-29-2005
X-Ftn-To: Jeremy Morton

"Jeremy Morton" <> wrote:
>Probably been mentioned before but I fail to see why Perl changed the
>'break' keyword to 'last', in loops. Bear with me on this - it seems
>semantically more accurate to say 'break' - you're immediately breaking out
>of the loop. 'last' makes it sound like the current loop will be the last,
>but not that the execution should be stopped immediately, whereas break
>makes it sound like the latter.


I'm guessing, "last" was picked because it's short and it sounds good along
with "next". Further, "break" could blur things for C people as his
"continue" friend is already used for something else.

[crosspost trimmed]


--
Matija
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-29-2005
"Jeremy Morton" <> wrote in
news:41fbaeb9$0$26027$:

[ Removed comp.lang.perl from newsgroups list ]

> Probably been mentioned before but I fail to see why Perl changed the
> 'break' keyword to 'last', in loops. Bear with me on this - it seems
> semantically more accurate to say 'break' -


I have a sneaking suspicion that you are trolling, but I will indulge
you anyway. You can think of 'last' as 'this is the last statement to be
excuted in this loop'. At least, that is why it made sense to me the
first time I started learning Perl.

> 'last' makes it sound like the current loop will be the last,


Your terminology is odd. How can the current loop be the last? How could
a statement in one loop affect whether or not other loops are excuted?

Are you saying, if I have:

my @animals = qw(cat dog dino);

for my $animal (@animals) {
last if $animal eq 'dog';
}

for my $animal (@animals) {
print uc $animal, "\n";
}

The second loop will not be excuted due to the 'last' statement in the
first loop.

I am going to recommend a visit to http://learn.perl.org/ as well as
reading perldoc -f last.

Hope this helps.

Sinan.
 
Reply With Quote
 
Jeremy Morton
Guest
Posts: n/a
 
      01-29-2005
Matija Papec <> wrote:
> X-Ftn-To: Jeremy Morton
>
> "Jeremy Morton" <> wrote:
>> Probably been mentioned before but I fail to see why Perl changed the
>> 'break' keyword to 'last', in loops. Bear with me on this - it seems
>> semantically more accurate to say 'break' - you're immediately
>> breaking out of the loop. 'last' makes it sound like the current
>> loop will be the last, but not that the execution should be stopped
>> immediately, whereas break makes it sound like the latter.

>
> I'm guessing, "last" was picked because it's short and it sounds good
> along with "next".


That's a good reason to choose a keyword, because it 'sounds good'? Heh.
It doesn't make semantic sense, and that's far more important IMHO.

> Further, "break" could blur things for C people as
> his "continue" friend is already used for something else.


Yes, I don't quite understand why 'continue' is used the way it is,
either... that doesn't make much sense. However i'd still rather have
'break' meaning what it does in most other languages i've come across. You
don't even have to do away with 'last'... just alias break to mean the same
thing.

--
Best regards,
Jeremy Morton (Jez)


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-29-2005
[defunct newsgroup comp.lang.perl trimmed]

Jeremy Morton <> wrote in comp.lang.perl.misc:
> Probably been mentioned before but I fail to see why Perl changed the
> 'break' keyword to 'last', in loops. Bear with me on this - it seems
> semantically more accurate to say 'break' - you're immediately breaking out
> of the loop. 'last' makes it sound like the current loop will be the last,
> but not that the execution should be stopped immediately, whereas break
> makes it sound like the latter.


It makes perfect sense when you name the loops appropriately:

line: while ( <DATA> ) {
next line if /^\s*#/;
chomp;
last line if $_ eq '__END__';
# ...
}

What could be clearer? That's the idea behind "next" and "last", even
if the label is not present.

> Where can I propose that this be changed, or break aliased to last, for Perl
> 6?


I don't believe that's still open for discussion. Look for something
appropriate at http://dev.perl.org/perl6/lists/ if you want to try anyway.

Anno
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      01-29-2005
On Sat, 29 Jan 2005, A. Sinan Unur wrote:

> "Jeremy Morton" <> wrote in
> > semantically more accurate to say 'break' -

>
> I have a sneaking suspicion that you are trolling,


I think you're being rather uncharitable. As far as I recall, "break"
/was/ the term used in BCPL for effectively the same purpose:

| causes execution to be resumed at the point just after
| the smallest textually enclosing loop command

it says in the old manual.

But the Perl usage is now well established, and the other arguments
on this thread are well taken. I doubt there's anything to be gained
by trying to change the Perl usage now, and I don't see any net
benefit in it.

all the best
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-29-2005
Jeremy Morton <> wrote in comp.lang.perl.misc:
> A. Sinan Unur <> wrote:
> > "Jeremy Morton" <> wrote in
> > news:41fbaeb9$0$26027$:
> >
> > [ Removed comp.lang.perl from newsgroups list ]
> >
> >> Probably been mentioned before but I fail to see why Perl changed the
> >> 'break' keyword to 'last', in loops. Bear with me on this - it seems
> >> semantically more accurate to say 'break' -

> >
> > I have a sneaking suspicion that you are trolling, but I will indulge

>
> Why does every genuine question to a Usenet group have to be a troll? See
> this is why I don't often post on bloody Usenet.


Take it or leave it.

> > you anyway. You can think of 'last' as 'this is the last statement to
> > be excuted in this loop'. At least, that is why it made sense to me
> > the first time I started learning Perl.

>
> Yes, that is the only way you can think about it validly. My point is that
> if 'next' is to mean 'execute the next iteration of this loop', it seems
> more natural for 'last' to mean 'make this the last iteration of this loop'
> as opposed to 'make this the last statement of this loop'.


You mean, the loop body continues to the final "}" before the loop
ends, like a delayed break? That's an interpretation I wouldn't
have thought of, just because it would be a rather awkward behavior.
(If you need the behavior, you can have it with the current working
of "last", but not easily the other way around.)

To me, "last" means last, enough, quit. Just like "next", it leaves
the normal sequence of execution immediately. Your interpretation
seems overly literal-minded and artificial to me.

> On the other
> hand, 'break' does sound natural as you're breaking out of the loop
> immediately by making this the last statement.


Only if you are already accustomed to its specific usage in C. "Break
out" isn't the first thing that comes to mind when I see "break" without
context. The step from the everyday meaning of "break" to the C-specific
one is no shorter than it is from the everyday "last" to the Perl-specific
meaning.

Anno
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      01-30-2005
"A. Sinan Unur" <> wrote:
> "Jeremy Morton" <> wrote
>
> > Probably been mentioned before but I fail to see why Perl changed the
> > 'break' keyword to 'last', in loops. Bear with me on this - it seems
> > semantically more accurate to say 'break' -

>
> I have a sneaking suspicion that you are trolling, but I will indulge
> you anyway. You can think of 'last' as 'this is the last statement to be
> excuted in this loop'. At least, that is why it made sense to me the
> first time I started learning Perl.
>
> > 'last' makes it sound like the current loop will be the last,

>
> Your terminology is odd. How can the current loop be the last?


By not executing another time.

> How could
> a statement in one loop affect whether or not other loops are excuted?


By doing a bunch of machine level crap behind the scenes, just the same
way that everything in a high-level language does everything.


> Are you saying, if I have:
>
> my @animals = qw(cat dog dino);
>
> for my $animal (@animals) {
> last if $animal eq 'dog';
> }
>
> for my $animal (@animals) {
> print uc $animal, "\n";
> }
>
> The second loop will not be excuted due to the 'last' statement in the
> first loop.


No, I'm pretty sure that is not what he is saying.

More like

for my $animal (@animals) {
last if $animal eq 'dog';
print $animal;
};

Would print 'dog', but not anything in @animals after 'dog'.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Jussi Mononen
Guest
Posts: n/a
 
      01-30-2005
> One thing from C that'd be really useful is to do away with the required
> braces in one-line if/then/else blocks, like this:
>
> if ($foo)
> func1($foo);
> elsif ($bar)
> func2($bar, $z);
> else
> return;


That is just prone to errors. What if you had to add some functionality into
one of the blocks? You would have to add the braces and forgetting to add
them is easy.

> I don't know if somebody already suggested this for Perl 6, but I think
> it'd be a nice improvement. That and a real switch statement.


http://www.perl6.org/doc/design/apo/...itch_statement

/jUSSi


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-30-2005
gargoyle <> writes:
> On 2005-01-29, Matija Papec <> wrote:
>> I'm guessing, "last" was picked because it's short and it sounds good along
>> with "next". Further, "break" could blur things for C people as his
>> "continue" friend is already used for something else.

>
> One thing from C that'd be really useful is to do away with the required
> braces in one-line if/then/else blocks, like this:
>
> if ($foo)
> func1($foo);
> elsif ($bar)
> func2($bar, $z);
> else
> return;


Please, no. One of the things I like best about Perl is that the
braces are required; I find it far more consistent than requiring them
only if there happens to be more than one statement.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
while-loops enter the last time after condition is filled? skanemupp@yahoo.se Python 4 04-07-2008 12:08 PM
skip last line in loops eight02645999@yahoo.com Python 15 12-18-2006 08:42 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
Perl loops should use break, not last Jeremy Morton Perl 1 01-30-2005 10:50 PM
MS Perl question -- how to use hacked script to work correctly(was Question on loops and return values or sumpin) James Perl Misc 12 12-20-2004 04:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57