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"
'@


3 comments:

Anonymous said...

You are correct on how to build a string with multiple lines instead of having one continuous string. But a Here doc in UNIX or PowerShell is meant to execute a command and give the input to prompts ... it's a script of its own but allows user input, not to build a string.

Unknown said...

*shrug* You can use it for whatever you want. I find it most useful for legibility in my scripts when a large or multi-line string is needed and you would otherwise end up with one really long string you have to scroll through in your editor or a bunch of concatenated strings for no reason other than formatting your code.

Anonymous said...

What about binary data?