Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

A pipeline exercise

Closed Thread
 
Thread Tools Display Modes
Old 10-19-2006   #1 (permalink)
Roman Kuzmin
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
Old 10-19-2006   #2 (permalink)
dreeschkind
Guest


 

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

Old 10-19-2006   #4 (permalink)
Roman Kuzmin
Guest


 

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

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

Closed Thread

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








Vistax64.com 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 2005-2008

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 47 48 49 50