![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | A pipeline exercise Run the code below: CODE: function f1 { begin {'begin 1'} process {$_} end {'end 1'} } function f2 { begin {'begin 2'} process {$_} end {'end 2'} } 1 | f1 | f2 OUTPUT: begin 2 begin 1 1 end 1 end 2 Hmm, perhaps at first it looks like the begin block of f2() is executed before the begin block of f1(). It is not true. Can you figure out what is actually happening? Tip: use << Set-PSDebug -trace 1 >> and run it again to see code and pipeline objects flow. -- Thanks, Roman |
My System Specs![]() |
| | #2 (permalink) |
| | RE: A pipeline exercise begin 2 --------- begin 1 1 end 1 --------- end 2 Tip 2: do _not_ use Set-PSDebug to cheat. :P -- greetings dreeschkind |
My System Specs![]() |
| | #3 (permalink) |
| | RE: A pipeline exercise And if you use write-host instead of the default formatter, you'll see the output in the order in which it is processed. Note that the "1" only gets printed once! function f1 { begin {write-host 'begin 1'} process {write-host $_} end {write-host 'end 1'} } function f2 { begin {write-host 'begin 2'} process {write-host $_} end {write-host 'end 2'} } PS> 1 | f1 | f2 begin 1 begin 2 1 end 1 end 2 -- greetings dreeschkind "dreeschkind" wrote: > begin 2 > --------- > begin 1 > 1 > end 1 > --------- > end 2 > > > Tip 2: do _not_ use Set-PSDebug to cheat. :P > > -- > greetings > dreeschkind |
My System Specs![]() |
| | #4 (permalink) |
| | RE: A pipeline exercise "dreeschkind" wrote: > ... > Note that the "1" only gets printed once! Interesting remark! And yet another puzzle: who writes '1' in this case? -- Thanks, Roman |
My System Specs![]() |
| | #5 (permalink) |
| | RE: A pipeline exercise "Roman Kuzmin" wrote: > "dreeschkind" wrote: > > ... > > Note that the "1" only gets printed once! > > Interesting remark! And yet another puzzle: who writes '1' in this case? Since only f1 has input that gets processed in the process block this can only be done by f1. Write-Host doen't write to the pipeline so f2 has no input to process. Btw.: Might be good idea to collect exercises / quizzes / gotchas like this. This could be included as an interactive tutorial to learn the inner workings of the shell, thus helping to avoid common mistakes! -- greetings dreeschkind |
My System Specs![]() |
| | #6 (permalink) |
| | RE: A pipeline exercise "dreeschkind" wrote: > Btw.: Might be good idea to collect exercises / quizzes / gotchas like this. > This could be included as an interactive tutorial to learn the inner workings > of the shell, thus helping to avoid common mistakes! This is definitely a good idea. Actually I hope that somebody is already doing that. So far this forum is the best source but relatively few people visit it or read everything in it. -- Thanks, Roman |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| credential in the pipeline | PowerShell | |||
| Using VM for Training Exercise | Virtual PC | |||
| object pipeline? | PowerShell | |||
| Brackets in the pipeline | PowerShell | |||
| Using the $_ pipeline with WMI | PowerShell | |||