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

About PowerShell's scope limiatations

Closed Thread
 
Thread Tools Display Modes
Old 07-03-2008   #1 (permalink)
RickB
Guest


 

About PowerShell's scope limiatations

Where/how can I advocate a fix for the scoping problem in
PowerShell?

What I really like about interpretted languages, and where I use
them the most, is when I need to dynamically generate code. Testing
automation is the biggest example but even things like the function
I posted a while back that signs and reloads a file is an example.

With Powershell, there doesn't seem to be a way to write a function
that can generate other functions that can be used by the caller
without making those functions global. As soon as you make them
global you need to worry about name conflicts and cleaning them up
and you loose the ability to have default versons of the functions
etc. Its a nightmare.

The only workaround to the problem seems to be to invoke the
generator with dot.

The HUGE drawback to dot is that every variable used by the
generator (including it's parameters) will be dumped into the
calling function's scope. So you need to be aware of every variable
used in such functions (and those they call) or use conventions to
avoid stepping on stuff. Most languages solved these problems 30+
years ago and PowerShell has forcably re-introducing them by
suggesting the use of such a lame and crude workaround as dot
sourcing.

PS 26> $a
190
PS 27> function test([string]$a){"value is '$a'"}
PS 28> test
value is ''
PS 29> $a
190
PS 30> . test
value is ''
PS 31> $a

PS 32>

Obviously it's too late to take the dot back.

But I don't see anything to prevent putting together a plan for
fixing the hole it does such a poor job filling.

I wouldn't complain if I didn't think there was a viable solution.
For example:

$Parent:Var =
Function $parent.newfcn ...

Since there is already a way to affect a parant's variables:

set-variable Var -scope 1 $local:Var

this isn't breaking any restrictions purposely put in place. It
does, however remove the barriers that exist in places where the
scope issue isn't so simple to circumvent - namely
functions/filters/cmdlets.

Naturally if there is already a way around the problem I'm all ears
but I'm sure that more users than just me would appreciate this
abiltiy.
Old 07-03-2008   #2 (permalink)
Tao Ma
Guest


 

Re: About PowerShell's scope limiatations

Hi RickB,
In PowerShell, function is just a special variable contained script block.
Just return a script block object to the caller and store it in a varialbe.
Try this:
PS C:\> function g() {
Quote:
Quote:

>> {
>> param($a = 10, $b = 20)
>> process { "$a, $b" }
>> }
>> }
>>
PS C:\> g
param($a = 10, $b = 20) process {
"$a, $b"
}

PS C:\> $sb = g
PS C:\> & $sb
10, 20
PS C:\> $a = 5; $b = 40
PS C:\> & $sb
10, 20
PS C:\> $a,$b
5
40

Best regards,
Tao Ma

"RickB" <rbielaws@xxxxxx>
??????:11781d51-1d87-49e3-b5ea-ddb9e97d26d6@xxxxxx
Quote:

> Where/how can I advocate a fix for the scoping problem in
> PowerShell?
>
> What I really like about interpretted languages, and where I use
> them the most, is when I need to dynamically generate code. Testing
> automation is the biggest example but even things like the function
> I posted a while back that signs and reloads a file is an example.
>
> With Powershell, there doesn't seem to be a way to write a function
> that can generate other functions that can be used by the caller
> without making those functions global. As soon as you make them
> global you need to worry about name conflicts and cleaning them up
> and you loose the ability to have default versons of the functions
> etc. Its a nightmare.
>
> The only workaround to the problem seems to be to invoke the
> generator with dot.
>
> The HUGE drawback to dot is that every variable used by the
> generator (including it's parameters) will be dumped into the
> calling function's scope. So you need to be aware of every variable
> used in such functions (and those they call) or use conventions to
> avoid stepping on stuff. Most languages solved these problems 30+
> years ago and PowerShell has forcably re-introducing them by
> suggesting the use of such a lame and crude workaround as dot
> sourcing.
>
> PS 26> $a
> 190
> PS 27> function test([string]$a){"value is '$a'"}
> PS 28> test
> value is ''
> PS 29> $a
> 190
> PS 30> . test
> value is ''
> PS 31> $a
>
> PS 32>
>
> Obviously it's too late to take the dot back.
>
> But I don't see anything to prevent putting together a plan for
> fixing the hole it does such a poor job filling.
>
> I wouldn't complain if I didn't think there was a viable solution.
> For example:
>
> $Parent:Var =
> Function $parent.newfcn ...
>
> Since there is already a way to affect a parant's variables:
>
> set-variable Var -scope 1 $local:Var
>
> this isn't breaking any restrictions purposely put in place. It
> does, however remove the barriers that exist in places where the
> scope issue isn't so simple to circumvent - namely
> functions/filters/cmdlets.
>
> Naturally if there is already a way around the problem I'm all ears
> but I'm sure that more users than just me would appreciate this
> abiltiy.

Old 07-04-2008   #3 (permalink)
sapienscripter's Avatar
Scripting Guru


  sapienscripter is offline

Re: About PowerShell's scope limiatations

You can create a global variable from your script or function:$global:var=1234. And some cmdlets like New-Alias have a -scope parameter to control where objects are created. Don't forget that PowerShell is also (and I might even say primarily) an interactive management shell. The fact that you can create scripts and functions only makes it more attractive for Windows administrators.

Depending on what you are trying to accomplish there may be PowerShell-specific solutions. Or it may be that PowerShell is simply the wrong tool for the job. As much as I enjoy working with it I don't use it for everything for this very reason.

And if you really want your comments about scope to reach Microsoft, I suggest posting them in the public PowerShell group. Otherwise post what you are trying to get to work here and let's see if there are solutions.
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a bug in the scope? Joel (Jaykul) Bennett PowerShell 1 2 Weeks Ago 11:33 AM
"Powershell's popularity is rocketing skywards" Roman Kuzmin PowerShell 1 07-07-2008 07:10 AM
What does 1# mean in Powershell's prompt O.Z. PowerShell 2 05-15-2008 06:25 AM
trap scope Bob Butler PowerShell 9 10-27-2007 08:41 PM
PowerShell's Default Start Directory Algernon PowerShell 3 05-26-2007 01:12 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