|
| Is this a bug with @args |
 |
Tue, 8 Apr 2008 11:17:20 -0700 |
When you get right down to it you could consider what I'm doing
here is using an enhanced version of Bruce Payette's CustomClass
code. From that view, I've got a class that manages a collection
of objects of other classes. I'm defining a PSScriptMethod for
the outer class that does nothing but pass it's arguments on
to a PSScriptMethod of the Default_ID object it's managing.
Unexpected token 'args' in expression or statement.
At C:\...\WindowsPowerShell\Watcher_Defs.ps1:248 char:59
+ method ID_In_Use {$this.Default_ID.Match @args <<<< }
Method ultimately boils down to this.
function method ([string]$name, [scriptblock] $script)
{new-object management.automation.PSScriptMethod $name,$script}
Match is defined similarly, as PSScriptMethod on Default_ID which is
a PSObject stored in a PSNoteProperty of my own object.
I'm not sure all that is relevant because I, perhaps naively, expect
@args to be useable (syntactically) anywhere I can use $args and
expect errors only if the content of $args is illegal in the context
|
| Post Reply
|
| Re: Is this a bug with @args |
 |
Tue, 8 Apr 2008 13:54:14 -0600 |
"RickB" <rbielaws@i1.net> wrote in message
news:91897cdb-f950-4b65-a011-d6a6b9eed5e3@24g2000hsh.googlegroups.com...
> When you get right down to it you could consider what I'm doing
> here is using an enhanced version of Bruce Payette's CustomClass
> code. From that view, I've got a class that manages a collection
> of objects of other classes. I'm defining a PSScriptMethod for
> the outer class that does nothing but pass it's arguments on
> to a PSScriptMethod of the Default_ID object it's managing.
>
> Unexpected token 'args' in expression or statement.
> At C:\...\WindowsPowerShell\Watcher_Defs.ps1:248 char:59
> + method ID_In_Use {$this.Default_ID.Match @args <<<< }
>
> Method ultimately boils down to this.
>
> function method ([string]$name, [scriptblock] $script)
> {new-object management.automation.PSScriptMethod $name,$script}
>
> Match is defined similarly, as PSScriptMethod on Default_ID which is
> a PSObject stored in a PSNoteProperty of my own object.
>
> I'm not sure all that is relevant because I, perhaps naively, expect
> @args to be useable (syntactically) anywhere I can use $args and
> expect errors only if the content of $args is illegal in the context
> it expands into.
I think the splatting operator in v2 CTP is meant for invocation of PowerShell
functions and not .NET methods like a PSScriptMethod e.g.:
31> function bar { $OFS=",";"Args are $args" }
32> function foo { bar @args }
33> foo 1 2
Args are 1,2
37> $list = 1,2,3
38> $obj = new-object psobject
39> $obj | add-member -type ScriptMethod -name Foo -value {
$OFS=",";"Args are $args"}
40> $obj.foo(1,2,3)
Args are 1,2,3
41> $obj.foo @list
Unexpected token 'list' in expression or statement.
At line:1 char:15
+ $obj.foo @list <<<<
42> $obj.foo(@list)
Use '$list' instead of '@list' when referencing variables in expressions.
'@list' can only be used as an argument to a
command.
At line:1 char:15
+ $obj.foo(@list <<<< )
--
Keith |
| Post Reply
|
| Re: Is this a bug with @args |
 |
Tue, 8 Apr 2008 14:12:22 -0700 |
On Apr 8, 2:54 pm, "Keith Hill [MVP]"
<r_keith_h...@mailhot.moc_no_spam_I> wrote:
> "RickB" <rbiel...@i1.net> wrote in
messagenews:91897cdb-f950-4b65-a011-d6a6b9eed5e3@24g2000hsh.googlegroups.com...
> > When you get right down to it you could consider what I'm doing
> > here is using an enhanced version of Bruce Payette's CustomClass
> > code. From that view, I've got a class that manages a collection
> > of objects of other classes. I'm defining a PSScriptMethod for
> > the outer class that does nothing but pass it's arguments on
> > to a PSScriptMethod of the Default_ID object it's managing.
>
> > Unexpected token 'args' in expression or statement.
> > At C:\...\WindowsPowerShell\Watcher_Defs.ps1:248 char:59
> > + method ID_In_Use {$this.Default_ID.Match @args <<<<
}
>
> > Method ultimately boils down to this.
>
> > function method ([string]$name, [scriptblock] $script)
> > {new-object management.automation.PSScriptMethod $name,$script}
>
> > Match is defined similarly, as PSScriptMethod on Default_ID which is
> > a PSObject stored in a PSNoteProperty of my own object.
>
> > I'm not sure all that is relevant because I, perhaps naively, expect
> > @args to be useable (syntactically) anywhere I can use $args and
> > expect errors only if the content of $args is illegal in the context
> > it expands into.
>
> I think the splatting operator in v2 CTP is meant for invocation of
PowerShell functions and not .NET methods like a PSScriptMethod e.g.:
>
> 31> function bar { $OFS=",";"Args are $args" }
> 32> function foo { bar @args }
> 33> foo 1 2
> Args are 1,2
>
> 37> $list = 1,2,3
> 38> $obj = new-object psobject
> 39> $obj | add-member -type ScriptMethod -name Foo -value {
$OFS=",";"Args are $args"}
> 40> $obj.foo(1,2,3)
> Args are 1,2,3
> 41> $obj.foo @list
> Unexpected token 'list' in expression or statement.
> At line:1 char:15
> + $obj.foo @list <<<<
> 42> $obj.foo(@list)
> Use '$list' instead of '@list' when referencing variables in expressions.
'@list' can only be used as an argument to a
> command.
> At line:1 char:15
> + $obj.foo(@list <<<< )
>
> --
> Keith- Hide quoted text -
>
> - Show quoted text -
I tried re-working the args so I wouldn't need to splat.
To my surprise, I still got an error.
Unexpected token 'args' in expression or statement.
At C:\...\WindowsPowerShell\FileWatcher_Defs.ps1:141 char:51
+ method ID_In_Use {$this.ID.Match $args <<<< }
After lots of wasted time it finally dawned on me that a
scriptblock is not a function. It can't take arguments.
Long story short, the proper syntax was:
|
| Post Reply
|
| Re: Is this a bug with @args |
 |
Tue, 8 Apr 2008 15:48:12 -0600 |
"RickB" <rbielaws@i1.net> wrote in message
news:465961af-b4c8-435d-99fd-9e50b641b848@d45g2000hsc.googlegroups.com...
>
> I tried re-working the args so I wouldn't need to splat.
> To my surprise, I still got an error.
>
> Unexpected token 'args' in expression or statement.
> At C:\...\WindowsPowerShell\FileWatcher_Defs.ps1:141 char:51
> + method ID_In_Use {$this.ID.Match $args <<<< }
>
> After lots of wasted time it finally dawned on me that a
> scriptblock is not a function. It can't take arguments.
> Long story short, the proper syntax was:
>
> method ID_In_Use {$this.Default_ID.Match($args)}
Weird because scriptblock can make use of $args e.g.:
31> $sb = {"args are $args"}
32> & $sb 1 2 3
args are 1 2 3
There must be something else going on here. Can you show a bit more of the
code that errors?
--
Keith
|
| Post Reply
|
| Re: Is this a bug with @args |
 |
Wed, 9 Apr 2008 05:58:37 -0700 |
On Apr 8, 4:48 pm, "Keith Hill [MVP]"
<r_keith_h...@mailhot.moc_no_spam_I> wrote:
> "RickB" <rbiel...@i1.net> wrote in message
>
> news:465961af-b4c8-435d-99fd-9e50b641b848@d45g2000hsc.googlegroups.com...
>
>
>
> > I tried re-working the args so I wouldn't need to splat.
> > To my surprise, I still got an error.
>
> > Unexpected token 'args' in expression or statement.
> > At C:\...\WindowsPowerShell\FileWatcher_Defs.ps1:141 char:51
> > + method ID_In_Use {$this.ID.Match $args <<<< }
>
> > After lots of wasted time it finally dawned on me that a
> > scriptblock is not a function. It can't take arguments.
> > Long story short, the proper syntax was:
>
> > method ID_In_Use {$this.Default_ID.Match($args)}
>
> Weird because scriptblock can make use of $args e.g.:
>
> 31> $sb = {"args are $args"}
> 32> & $sb 1 2 3
> args are 1 2 3
>
> There must be something else going on here. Can you show a bit more of
the
> code that errors?
>
> --
> Keith
After studying my mistake I learned quite a bit.
I'm not exactly sure if a straight ScriptBlock and a PSScriptMethod
are
the same thing. I was using a PSScriptMethod. It looks like a
wrapper
around a ScriptBlock and so I used the word ScriptBlock in error.
Anyway it turns out that a PSScriptMethod can work differently when
used
directly as opposed to being used while attached to a PSObject
For example a PSScriptMethod can be used like a ScriptBlock if you
get the syntax correct.
{"args are $args"}.invoke(1,2,3)
PS > $a = new-object management.automation.PSScriptMethod 'arg_fcn',
{"args are $args"}
PS > $a.script.invoke(1,2,3)
args are 1 2 3
PS > & $a 1 2 3
The term 'System.Object arg_fcn();' is not recognized as a cmdlet,
function, operable program, or script file. Verify the term and try
again.
At line:1 char:2
+ & <<<< $a 1 2 3
PS > & $a.script 1 2 3
args are 1 2 3
Apparently the wrapper I talked about allows this too
PS > $a.invoke(1,2,3)
args are 1 2 3
Inside a PSObject another, similar, shortcut is available.
PS > $i = new-object management.automation.psobject
PS > $i.psobject.members.add($a)
PS > $i.arg_fcn(1,2,3)
args are 1 2 3
PS > $i.arg_fcn.script.invoke(1,2,3)
args are 1 2 3
PS > $i.arg_fcn.invoke(1,2,3)
args are 1 2 3
To use & here though I still need to resort to this:
PS > & $i.arg_fcn.script 1 2 3
|
| Post Reply
|
|
|