Welcome to MSDN Blogs Sign in | Join | Help

Difference between $() and ()

 Today, I was writing some script to play around hash declaration.   My line of script to declare hash was

 

MSH>$a = @{(‘foo’ + ‘bar’) = 5)}

MSH>$a.foobar

5

MSH>

Then, I tried

 

MSH>$a = @{$(‘foo’ + ‘bar’)=5}

MSH>$a.foobar

5

MSH>

 

That worked too.  So, what’s the difference between  ()  and $()

 

Answer is () is used only for expressions whereas $() is used for statement list.  The above example works because ‘foo’ + ‘bar’ is treated as statement in $() case.

 

To clarify the point

 

MSH>$a = @{ $( $a=’foo’ ;   $a += ‘bar’ ;  $a) = 5}

MSH>$a.foobar

5

MSH>

 

The above  will work fine.  However,

 

MSH> $a = @{ ( $a=’foo’ ;   $a += ‘bar’ ;  $a) = 5}

 

will fail because  parser expects an expression, but  finds  a statement list.

Published Friday, July 15, 2005 4:12 PM by arulk

Comments

# Time for another Nomad Link Round-Up as well


Working with the new Monad beta -- getting settled in
Param statement, and new-object
Difference...
Monday, August 22, 2005 10:07 AM by Life, Universe and Everything according to Dirk

# how does $() work? | keyongtech

Thursday, January 22, 2009 12:26 AM by how does $() work? | keyongtech
Anonymous comments are disabled
 
Page view tracker