![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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 | 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. |
| | #2 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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() {
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
| ||||||||||||||||||||||||||||||||||||
| | #3 (permalink) |
| Scripting Guru | 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. |
| |
| |
![]() |
| 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 |