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 - A pipeline exercise

Reply
 
Old 10-19-2006   #1 (permalink)
Roman Kuzmin


 
 

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 SpecsSystem Spec
Old 10-19-2006   #2 (permalink)
dreeschkind


 
 

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 SpecsSystem Spec
Old 10-19-2006   #3 (permalink)
dreeschkind


 
 

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 SpecsSystem Spec
Old 10-19-2006   #4 (permalink)
Roman Kuzmin


 
 

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 SpecsSystem Spec
Old 10-19-2006   #5 (permalink)
dreeschkind


 
 

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 SpecsSystem Spec
Old 10-19-2006   #6 (permalink)
Roman Kuzmin


 
 

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 SpecsSystem Spec
Reply

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


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