Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Why can't a foreach block begin a pipeline?

Reply
 
Old 10-25-2006   #1 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Why can't a foreach block begin a pipeline?

Although some of the constraints on the internal foreach construct exist for
understandable reasons, I can't see the reason that output generated within
it is not treated as an output stream directly. Can anyone explain this?

For example, the following causes an error:
PS> foreach($i in 1,2,3){$i} | gm
An empty pipe element is not permitted.
At line:1 char:27
+ foreach($i in 1,2,3){$i} | <<<< gm
I could see this to overcome potential difficulties if you could move the
enumerator backwards, but you can't. Even if you could, you could still
forestall any problems with releasing objects by simply completing the block
before emitting the output.

The following _does_ work and takes minimal extra effort, so this isn't a
major inconvenience; I'm just curious about why it is necessary:

PS> $(foreach($i in 1,2,3){$i}) | gm



My System SpecsSystem Spec
Old 10-25-2006   #2 (permalink)
George Xie [MSFT]


 
 

Re: Why can't a foreach block begin a pipeline?

The grammar for pipeline statement is that it is made up of a list of
expression or command connected with pipes. Foreach, on the other hand, is a
statement. So the command below is eventually the same as following two
commands,

foreach($i in 1,2,3){$i}
| gm

The reason for this behaviour is purely a design decision so that clean cut
between different layers of language structure.
--
George Xie [MSFT]
Microsoft Command Shell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
news:%23W5sPUE%23GHA.4376@TK2MSFTNGP03.phx.gbl...
> Although some of the constraints on the internal foreach construct exist
> for understandable reasons, I can't see the reason that output generated
> within it is not treated as an output stream directly. Can anyone explain
> this?
>
> For example, the following causes an error:
> PS> foreach($i in 1,2,3){$i} | gm
> An empty pipe element is not permitted.
> At line:1 char:27
> + foreach($i in 1,2,3){$i} | <<<< gm
> I could see this to overcome potential difficulties if you could move the
> enumerator backwards, but you can't. Even if you could, you could still
> forestall any problems with releasing objects by simply completing the
> block before emitting the output.
>
> The following _does_ work and takes minimal extra effort, so this isn't a
> major inconvenience; I'm just curious about why it is necessary:
>
> PS> $(foreach($i in 1,2,3){$i}) | gm
>



My System SpecsSystem Spec
Old 10-25-2006   #3 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: Why can't a foreach block begin a pipeline?

OK. That's a pretty good reason.

"George Xie [MSFT]" <gxie@online.microsoft.com> wrote in message
news:%23KIe%23tF%23GHA.924@TK2MSFTNGP03.phx.gbl...
> The grammar for pipeline statement is that it is made up of a list of
> expression or command connected with pipes. Foreach, on the other hand, is
> a statement. So the command below is eventually the same as following two
> commands,
>
> foreach($i in 1,2,3){$i}
> | gm
>
> The reason for this behaviour is purely a design decision so that clean
> cut between different layers of language structure.
> --
> George Xie [MSFT]
> Microsoft Command Shell Development
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
> "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
> news:%23W5sPUE%23GHA.4376@TK2MSFTNGP03.phx.gbl...
>> Although some of the constraints on the internal foreach construct exist
>> for understandable reasons, I can't see the reason that output generated
>> within it is not treated as an output stream directly. Can anyone explain
>> this?
>>
>> For example, the following causes an error:
>> PS> foreach($i in 1,2,3){$i} | gm
>> An empty pipe element is not permitted.
>> At line:1 char:27
>> + foreach($i in 1,2,3){$i} | <<<< gm
>> I could see this to overcome potential difficulties if you could move the
>> enumerator backwards, but you can't. Even if you could, you could still
>> forestall any problems with releasing objects by simply completing the
>> block before emitting the output.
>>
>> The following _does_ work and takes minimal extra effort, so this isn't a
>> major inconvenience; I'm just curious about why it is necessary:
>>
>> PS> $(foreach($i in 1,2,3){$i}) | gm
>>

>
>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Where to begin General Discussion
foreach on a null interates the block? PowerShell
foreach, foreach-object & begin/process/end scriptblock clauses... PowerShell
Difference in semantics of for, foreach and foreach-object PowerShell
possible bug with using the $foreach keyword inside foreach loops.. PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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