![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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 |
| | #3 (permalink) |
| Guest | 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 |
| | #5 (permalink) |
| Guest | 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 |
| | #6 (permalink) |
| Guest | 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 |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pipeline and ScriptBlocks | Shane | PowerShell | 8 | 01-21-2008 10:27 PM |
| Executenonquery in pipeline | gurbao | PowerShell | 5 | 11-16-2007 12:14 PM |
| Brackets in the pipeline | Dmitry Sotnikov | PowerShell | 2 | 09-06-2007 10:33 AM |
| Using the $_ pipeline with WMI | Larry R | PowerShell | 2 | 04-27-2007 09:27 AM |
| pipeline timeout | William Stacey [C# MVP] | PowerShell | 0 | 04-09-2007 03:26 PM |