Showing posts with label Here Docs. Show all posts
Showing posts with label Here Docs. Show all posts

Monday, July 28, 2008

Here Docs in PowerShell

Those of you that are familiar with Unix-style shell scripting or Perl may be familiar with the concept of a HERE doc.  This example from Perl may help explain the name:

my $multiline_string = <<HERE;
This
  is
    my
      string.
HERE


The idea is that once you start the Here Doc, everything you type is taken as part of your string until you tell it that it's time to stop, in this case by putting HERE on its own line.

In PowerShell you can do something similar, using @' and '@ or @" and "@.  Everything from the @' or @" to the '@ or "@ is considered part of the same string.

PS C:\Users\tojo2000\Documents> $multiline_string = @'
>> This
>>   is
>>     my
>>       System.String.
>> '@
>>
PS C:\Users\tojo2000\Documents\> $multiline_string
This
  is
    my
      System.String.

Note that the multiline strings use single and double quotes to control variable interpolation just like regular single and double quotes.  Any variables in a multiline double-quoted string will be expanded.

Why would you want to do this?  There are a lot of situations in which this can make your strings a lot more readable.  As an example, let's say you have a SQL query:

$sql = 'SELECT * FROM employees INNER JOIN parking ON parking.emp_id = employees.emp_id WHERE parking.size = "Compact"';

compare that to this:

$sql = @'
SELECT * FROM employees
INNER JOIN parking
  ON parking.emp_id = employees.emp_id
WHERE parking.size = "Compact"
'@