[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why should `break' and `continue' in functions not break loops runni

From: L A Walsh
Subject: Re: Why should `break' and `continue' in functions not break loops running outside of the function?
Date: Sat, 30 Oct 2021 17:38:11 -0700
User-agent: Thunderbird (Windows/20100228)

On 2021/10/30 09:07, Robert Elz wrote:
oguzismailuysal@gmail.com said:
   | `break' is not a keyword in the shell, but a special command.

That's true. However, 99%**1 of script writers don't see it that way,g they believe it is just like "if" or
 "while" or "done" or "return".

That's why the "This is counterintuitive." I would guess - in most
cases, the issue isn't quite like yours, but more like

   Something that supports break/continue being dynamically
scoped: the variables used in the loop are dynamically
scoped.  As long as the function can access the local variables,
it is "part" of the loop.

   Bash/shell provides dynamic scoping, by default, for
referenced (but undeclared) variables.  So why wouldn't
someone expect the functions that can change all the variables
in the loop to also be able to modify loop params?

ps: the NetBSD shell continues to work the way that you want, and does so by deliberate choice**2 - our test
suite has a whole stack of tests to make sure this
continues to all work "correctly" (doesn't
accidentally get changed).
So you are saying that NetBSD shell script writers are less
than 1% of script writers?  Is NetBSD's market share that
low?  Just curious.

Regardless -- it points, at least, to it being something
that I would think should be shopt'd if nothing else.

I find the inconsistent application of loop parameters
to be, at least, a wart -- i.e. loop variables are dynamically
propagated to called functions, but loop control "verbs" aren't.

Perl is a bit schizoid in this area:

use warnings; use strict; use P;
my $x;
sub foo() {
   if ($x>=2 && $x<4) { next; }
   if ($x==5) { $x=9; }
   if ($x>=11) { last;}
for ($x=0;$x<20;++$x){
   P "b4 foo x=%s", $x;
   P "after foo x=%s", $x;
results in dynamically scoped execution with commentary:

b4 foo x=0
after foo x=0
b4 foo x=1
after foo x=1
b4 foo x=2
Exiting subroutine via next at /tmp/lex.pl line 5.
b4 foo x=3
Exiting subroutine via next at /tmp/lex.pl line 5.
b4 foo x=4
after foo x=4
b4 foo x=5
after foo x=9
b4 foo x=10
after foo x=10
b4 foo x=11
Exiting subroutine via last at /tmp/lex.pl line 7.

I think a shopt would be more flexible.  Having
loop vars be dynamic, but verbs not, seem inconsistent.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]