![]() |
Perl loops should use break, not last
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) |
Re: Perl loops should use break, not last
X-Ftn-To: Jeremy Morton
"Jeremy Morton" <ask@me.com> 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 |
Re: Perl loops should use break, not last
"Jeremy Morton" <ask@me.com> wrote in
news:41fbaeb9$0$26027$fa0fcedb@news.zen.co.uk: [ 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. |
Re: Perl loops should use break, not last
Matija Papec <perl@my-header.org> wrote:
> X-Ftn-To: Jeremy Morton > > "Jeremy Morton" <ask@me.com> 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) |
Re: Perl loops should use break, not last
[defunct newsgroup comp.lang.perl trimmed]
Jeremy Morton <ask@me.com> 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 |
Re: Perl loops should use break, not last
On Sat, 29 Jan 2005, A. Sinan Unur wrote:
> "Jeremy Morton" <ask@me.com> 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 |
Re: Perl loops should use break, not last
Jeremy Morton <ask@me.com> wrote in comp.lang.perl.misc:
> A. Sinan Unur <1usa@llenroc.ude.invalid> wrote: > > "Jeremy Morton" <ask@me.com> wrote in > > news:41fbaeb9$0$26027$fa0fcedb@news.zen.co.uk: > > > > [ 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 |
Re: Perl loops should use break, not last
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
> "Jeremy Morton" <ask@me.com> 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 |
Re: Perl loops should use break, not last
> 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 |
Re: Perl loops should use break, not last
gargoyle <gargoyle@no.spam> writes:
> On 2005-01-29, Matija Papec <perl@my-header.org> 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-u@mib.org <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. |
| All times are GMT. The time now is 09:55 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.