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 does even statement with a value get stuffed into the outputpipeline?

Reply
 
Old 08-25-2006   #1 (permalink)
Adam Milazzo


 
 

why does even statement with a value get stuffed into the outputpipeline?

Consider this:

function foo
{
$list = new-object Collections.ArrayList
$list.add(10)
$list.add(20)
return $list
}

% foo
0
1
10
20

% foo | measure-object
Count: 3
....

It's actually returning an array of 3 objects. The first is the return
value of $list.add(10) (which is 0). The second is the return value of
$list.add(20) (which is 1). The third is the ArrayList object itself.

This is really annoying! Not to mention very unintuitive. In my opinion,
you should be required to use write-object to put things into the
pipeline, with the exception of "return <expression>", which would be
equivalent to "<expression> | write-object; return".

My System SpecsSystem Spec
Old 08-25-2006   #2 (permalink)
=?Utf-8?B?S2FybCBQcm9zc2Vy?=


 
 

RE: why does even statement with a value get stuffed into the output p

i don't have powershell on this computer so my syntax maybe off a little. But
i'll explain what is going on

anything that is returned from a function, or an expression gets put into
the pipeline.. so if you run

1..10
"hello"

, you'll get an array of 10 integers and a string.

being a shell, and wanting the user to be able to do thinks quickly they
didn't want to have to explicting do write-object...
but what is happening with your situation is that arraylist.add method (if
you look it up in the dotnet framework help) returns a value.. (the index in
the arraylist if i am not mistaken.. so powershell is adding this to the
pipeline.. you can CONSUME that result so that powershell doesn't add it to t
he pipeline in a number of ways

the first would be to put it into a variable

$local:dummy = $list.add(20)

however that is not so elegant.. there is an easier way and that it to
typecast the result as void, and i think this is the syntax

[void]$list.add(20)

also i don't think the return keyword is needed (but i could be wrong.. you
could write this function like this:

function foo
{
$list = new-object Collections.ArrayList
[void]$list.add(10)
[void]$list.add(20)
$list
}

hope that helps.
-------
Karl Prosser
maker of powershell analyzer
http://www.karlprosser.com/coder

My System SpecsSystem Spec
Old 08-25-2006   #3 (permalink)
Adam Milazzo


 
 

Re: why does even statement with a value get stuffed into the outputp

Karl Prosser wrote:
> i don't have powershell on this computer so my syntax maybe off a little. But
> i'll explain what is going on
>
> anything that is returned from a function, or an expression gets put into
> the pipeline.. so if you run
>
> 1..10
> "hello"
>
> , you'll get an array of 10 integers and a string...


I realize this is how it is, but I don't think this is how it should be.
It seems that the majority of statements in a function are not intended
to be pushed into the pipeline. This means that the majority of
statements need to be redirected for the sake of the few (usually one)
that I actually want pushed into the pipeline.
My System SpecsSystem Spec
Old 08-25-2006   #4 (permalink)
Jouko Kynsijärvi


 
 

Re: why does even statement with a value get stuffed into the output p

Adam Milazzo wrote:
> I realize this is how it is, but I don't think this is how it should
> be. It seems that the majority of statements in a function are not
> intended to be pushed into the pipeline. This means that the majority
> of statements need to be redirected for the sake of the few (usually
> one) that I actually want pushed into the pipeline.


There was a long discussion about this during the earlier betas. If i
remember correctly one of the suggestions was to use two keywords, something
like "function" and "scriptlet" etc. where former would not automatically
emit anything to the pipeline, and latter would just like function does now.
Since PS v1 is so close of beeing done, i think we'll have to live with the
current situation.

One way to easily discard any pipeline output without adding [void] etc to
every line producing unwanted output is to use something like this:

function foo {
$list = new-object Collections.ArrayList
&{
$list.add(10)
$list.add(20)
} | out-null
$list
}


My System SpecsSystem Spec
Old 08-26-2006   #5 (permalink)
Jeffrey Snover [MSFT]


 
 

Re: why does even statement with a value get stuffed into the output p

A few of us where actually brainstorming just such a concept a few weeks
ago. There are pros and cons to the idea but please file a feature request
for this.

BTW - you might ask, "if you where thinking about doing it, why do I need to
file a feature request?". The answer is that we put potential features into
different buckets depending upon where they came from. Features requested
from users are (as a general rule) in a higher priority bucket.

--
Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx


My System SpecsSystem Spec
Old 08-27-2006   #6 (permalink)
Adam Milazzo


 
 

Re: why does even statement with a value get stuffed into the outputp

Jeffrey Snover [MSFT] wrote:
> A few of us where actually brainstorming just such a concept a few weeks
> ago. There are pros and cons to the idea but please file a feature request
> for this.

Okay, I did. Thanks. :-)
My System SpecsSystem Spec
Old 08-30-2006   #7 (permalink)
Karl Prosser


 
 

Re: why does even statement with a value get stuffed into the output p

maybe a way to do with, would be a keyword with a scope, and everything
within that scope gets consumed (not returned, or put into the pipeline)
unless explicitly told to?
I choose the void keyword just an example, but beign a keyword it would be
included in base documentation, and peoples books etc.. and their is
consistency with the typecast [void]

what do you think
i.e
function foo
{
void {
$list = new-object Collections.ArrayList
$list.add(10)
$list.add(20)
return $list
}
}


My System SpecsSystem Spec
Old 08-31-2006   #8 (permalink)
Adam Milazzo


 
 

Re: why does even statement with a value get stuffed into the outputp

Karl Prosser wrote:
> maybe a way to do with, would be a keyword with a scope, and everything
> within that scope gets consumed (not returned, or put into the pipeline)
> unless explicitly told to?
> I choose the void keyword just an example, but beign a keyword it would be
> included in base documentation, and peoples books etc.. and their is
> consistency with the typecast [void]
>
> what do you think
> i.e
> function foo
> {
> void {
> $list = new-object Collections.ArrayList
> $list.add(10)
> $list.add(20)
> return $list
> }
> }


Perhaps this could be done as a "void" function that takes a script block...

function void([scriptblock]$block)
{
&$block | out-null
}

function foo
{
$local:list = new-object Collections.ArrayList
void {
$list.add(10)
$list.add(20)
}
return $list
}


This works, but it's not much of an improvement over:

function foo
{
$local:list = new-object Collections.ArrayList
$list.add(10) | out-null
$list.add(20) | out-null
return $list
}

or

function foo
{
$local:list = new-object Collections.ArrayList
($list.add(10); $list.add(20)) | out-null
return $list
}


But it's a neat idea. :-)
My System SpecsSystem Spec
Old 08-31-2006   #9 (permalink)
Jeffrey Snover [MSFT]


 
 

Re: why does even statement with a value get stuffed into the output p

> function void([scriptblock]$block)
> {
> &$block | out-null
> }


That IS a neat idea.

--
Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
i stuffed up the boot up procedure. Vista General
Indexing Completely Stuffed Vista file management
Has MS stuffed Activation Again ? Vista General
Vista stuffed Vista installation & setup
Vista stuffed Vista installation & setup


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