You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Foreach loop using | % {} syntax doesn't support continue #3879
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics
break and continue work dynamically - meaning the break/continue statement searches for an appropriate loop to break from at runtime. For example:
functionfoo {
break outer # No loop in this function!
}
:outer foreach ($iin1..100) {
$iforeach ($jin100..1000) {
foo
}
}
This will output a single number, breaking out of the outer loop.
The unfortunate thing is that break sort of works as people expect with ForEach-Object, but in my opinion, it's sort of an accident that falls out from the implementation.
Under the covers, the break turns into an exception (always V2 or earlier, V3 onwards if not lexically within a loop statement), the exception is always silent (because you don't really want to think of break as an exception, so it's silent even if we don't find a matching loop.
Reacted by Michael Klement, alx9r, Jeff Kolb and geogboe
You can think of a script block as an (anonymous) function, so you can always return from it to proceed to the next object in the pipeline.
However, there is currently no analog to break for prematurely exiting a pipeline.
Overcoming this limitation is the subject of #3821.
Using the behavior described by Jason Shirk (@lzybkr), you can use the following (cumbersome) workaround:
# Exit the pipeline after the first 2 items.do {
@("1","2","3") |% { $_; if($_-eq"2") { break } }
} while ($false) # dummy loop to break out of
Note that you don't need the dummy loop on the command line, but if you neglected to use it in a script, it would break out of whatever enclosing loop there is, if any.
If there is none, the entire script is exited.
Thanks for all your inputs. I didn't even consider using return. It's good to know that another issue has been raised regarding this though and hopefully someone can add this little improvement to the language. thanks
It's possible somebody is relying on the existing semantics, so changing that might not be a good idea.
I've considered a strict mode error when break or continue is used in a way that turns into the non-local goto - the error message could suggest alternatives like return.
You should regard this as Linq ForEach or JavaScript forEach(...). What you feed to ForEach-Object cmdlet is a ScriptBlock (delegate or function). Just as you won't
I'm a little late to the party looking at the date (I came past this thread while looking for something else), but thought I might throw this in for the benefit of those on future expeditions...
My suggestion would be to look past the more terse lingual subtext (C#-like constructs) and use the pipeline in a more "intent-wise fashion" = i.e. "script-like"; I have found personally that while PS is sitting on .NET - not everything translates smoothly in the interpreter to a C# equivalent. So sometimes stepping back and "saying the same thing differently" can help expedite.
@("1","2","3") | ?{$_ -notmatch "2"} | out-host
This outputs 1 and 3 as expected (in response to the original "questioned" construct); and in this case, -ne works as well if preferred over the Regex equivalent shown in the example.
Is it worth discussing whether behaviour of keywords like continue and break should be scope-restricted? After all, currently they literally cannot be handled whatsoever as several folks here have stated.
This is... unsettling, that calling a function from a module you've just downloaded could conceivably halt all running scripts right up to the global level, with no possible way of being able to account for it. This is what actual terminating errors are for in PowerShell, and we're (sensibly) given ways to handle them appropriately.
break and continue are the exception (badum/tss) here, and I don't think it's in a good way. Wouldn't it make a lot more sense for it to simply terminate the parent script block, function, or script -- but only the immediate level of execution? Searching beyond the parent function, script block, or script for something to break seems rife for exploitability and just generally breaking an awful lot of code that would and should otherwise be perfectly capable of handling similar terminating errors. 😦
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 23, 2023
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics
Activity
powercode commentedon May 30, 2017
Well, it isn't a loop.
You are using foreach-object cmdlet in a pipeline.
If
continueshould be valid there, it should also be valid inAnd that is not obviously correct.
lzybkr commentedon May 30, 2017
breakandcontinuework dynamically - meaning thebreak/continuestatement searches for an appropriate loop to break from at runtime. For example:This will output a single number, breaking out of the outer loop.
The unfortunate thing is that
breaksort of works as people expect withForEach-Object, but in my opinion, it's sort of an accident that falls out from the implementation.Under the covers, the
breakturns into an exception (always V2 or earlier, V3 onwards if not lexically within a loop statement), the exception is always silent (because you don't really want to think ofbreakas an exception, so it's silent even if we don't find a matching loop.powercode commentedon May 30, 2017
Cool, and really unexpected :)
mklement0 commentedon May 30, 2017
Ryan Bartram (@rdbartram):
In a
%(ForEach-Object) script block in a pipeline,returnis the equivalent ofcontinue:You can think of a script block as an (anonymous) function, so you can always
returnfrom it to proceed to the next object in the pipeline.However, there is currently no analog to
breakfor prematurely exiting a pipeline.Overcoming this limitation is the subject of #3821.
Using the behavior described by Jason Shirk (@lzybkr), you can use the following (cumbersome) workaround:
Note that you don't need the dummy loop on the command line, but if you neglected to use it in a script, it would break out of whatever enclosing loop there is, if any.
If there is none, the entire script is exited.
rdbartram commentedon May 30, 2017
Thanks for all your inputs. I didn't even consider using return. It's good to know that another issue has been raised regarding this though and hopefully someone can add this little improvement to the language. thanks
lzybkr commentedon May 31, 2017
It's possible somebody is relying on the existing semantics, so changing that might not be a good idea.
I've considered a strict mode error when
breakorcontinueis used in a way that turns into the non-local goto - the error message could suggest alternatives likereturn.rkeithhill commentedon May 31, 2017
Jason Shirk (@lzybkr) Might also be good to have a PSSA rule for that - if it doesn't already exist.
cc Kapil Borle (@kapilmb)
GeeLaw commentedon May 31, 2017
You should regard this as Linq ForEach or JavaScript forEach(...). What you feed to
ForEach-Objectcmdlet is aScriptBlock(delegateorfunction). Just as you won'tYou don't
continueorbreakin a bareScriptBlock. The scope is isolated.RobR14 commentedon Dec 11, 2018
I'm a little late to the party looking at the date (I came past this thread while looking for something else), but thought I might throw this in for the benefit of those on future expeditions...
My suggestion would be to look past the more terse lingual subtext (C#-like constructs) and use the pipeline in a more "intent-wise fashion" = i.e. "script-like"; I have found personally that while PS is sitting on .NET - not everything translates smoothly in the interpreter to a C# equivalent. So sometimes stepping back and "saying the same thing differently" can help expedite.
@("1","2","3") | ?{$_ -notmatch "2"} | out-host
This outputs 1 and 3 as expected (in response to the original "questioned" construct); and in this case, -ne works as well if preferred over the Regex equivalent shown in the example.
vexx32 commentedon Dec 11, 2018
Is it worth discussing whether behaviour of keywords like
continueandbreakshould be scope-restricted? After all, currently they literally cannot be handled whatsoever as several folks here have stated.This is... unsettling, that calling a function from a module you've just downloaded could conceivably halt all running scripts right up to the global level, with no possible way of being able to account for it. This is what actual terminating errors are for in PowerShell, and we're (sensibly) given ways to handle them appropriately.
breakandcontinueare the exception (badum/tss) here, and I don't think it's in a good way. Wouldn't it make a lot more sense for it to simply terminate the parent script block, function, or script -- but only the immediate level of execution? Searching beyond the parent function, script block, or script for something to break seems rife for exploitability and just generally breaking an awful lot of code that would and should otherwise be perfectly capable of handling similar terminating errors. 😦microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.