<!--  vim: set sw=2 sts=2 et ft=docbk:

  Part of the A-A-P recipe executive: Assignments

  Copyright (C) 2002-2003 Stichting NLnet Labs
  Permission to copy and use this file is specified in the file COPYING.
  If this file is missing you can find it here: http://www.a-a-p.org/COPYING

-->

<bridgehead>Assignment</bridgehead>

<para>
overview:
  <informaltable frame='none'>
    <tgroup cols='2'>
      <colspec colwidth="150"/>
      <tbody>
        <row>
   <entry>var = value</entry>
   <entry>assign</entry>
    </row>
    <row>
   <entry>var += value</entry>
   <entry>append (assign if not set yet)</entry>
    </row>
    <row>
   <entry>var ?= value</entry>
   <entry>assign only when not set yet</entry>
    </row>
    <row>
   <entry>var $= value</entry>
   <entry>assign, evaluate when used</entry>
    </row>
    <row>
   <entry>var $+= value</entry>
   <entry>append, evaluate when used</entry>
    </row>
    <row>
   <entry>var $?= value</entry>
   <entry>assign only when not set, evaluate when used</entry>
        </row>
      </tbody>
    </tgroup>
  </informaltable>
</para>

<para>
Assignment with "+=" or "$+=" appends the argument as a separate item.  This
is actually done by inserting a space.  But when the variable wasn't set yet
and when it is empty it works like a normal assignment:
</para>

<programlisting>
        VAR += something
</programlisting>

<para>
is equal to:
</para>

<programlisting>
        @if globals().get("_no.VAR"):
        @   VAR = _no.VAR + " " + "something"
        @else:
        @   VAR = "something"
</programlisting>

<para>
Assignment with "?=" only does the assignment when the variable wasn't set
yet.  A variable that was set to an empty string also counts as being set.
Thus when using "aap VAR=" the empty value overrules the value set with "?=".
</para>

<programlisting>
        VAR ?= something
</programlisting>

<para>
is equal to:
</para>

<programlisting>
        @if not globals().has_key("_no.VAR"):
            VAR = something
</programlisting>

<para>
When using "$=", "$+=" or "$?=" variables in the argument are not evaluated at
the time of assignment, but this is done when the variable is used.  The
expansion is done in the scope where it is used, thus the result may depend on
when and where the variable is used..

<programlisting>
        VAR = 1
        TT $= $VAR
        VAR = 2
        :print $TT
</programlisting>

prints "2".
</para>

<para>
A variable with delayed evaluation cannot be used directly in Python code,
because it is set the the class ExpandVar.  See the 
<link linkend="python-var2string">var2string()</link> function for
expanding the variable in Python code.
</para>

<para>
When first setting a variable with "$=" and later appending with "+=" the
evaluation is done before the new value is appended:

<programlisting>
        VAR = 1
        TT $= $VAR
        TT += 2
        VAR = 3
        :print $TT
</programlisting>

prints "1 2"
</para>

<para>
Note that evaluating a python expressions in `` is not postponed.
</para>


<bridgehead>Block Assignment</bridgehead>

<para>
The normal assignment command uses a single line of text.  When broken into
several lines they are joined together, just like with other commands.  $BR can
be used to insert a line break.  Example:
</para>

<programlisting>
        foo = first line$BR
                second line$BR
                third line $BR
</programlisting>

<para>
The block assignment keeps the line breaks as they are.  The same example but
using a block assignment:
</para>

<programlisting>
        foo &lt;&lt; EOF
          first line
          second line
          third line 
            EOF
</programlisting>

<para>
The generic format is:
</para>

<programlisting>
        {var} &lt;&lt; {term}
        line1
        ...
        {term}
</programlisting>

<para>
{term} can be any string without white space.  The block ends when {term} is
found in a line by itself, optionally preceded by white space and followed by
white space and a comment.
</para>

<para>
The amount of indent to be removed from all the lines is set by the first
line.  When the first line should start with white space use $( ).
</para>

<para>
All the variations of the assignment command can be used:

  <informaltable frame='none'>
    <tgroup cols='2'>
      <colspec colwidth="150"/>
      <tbody>
        <row>
   <entry>var &lt;&lt; term</entry>
   <entry>assign</entry>
        </row>
        <row>
   <entry>var +&lt;&lt; term</entry>
   <entry>append (assign if not set yet)</entry>
        </row>
        <row>
   <entry>var ?&lt;&lt; term</entry>
   <entry>only assign when not set yet</entry>
        </row>
        <row>
   <entry>var $&lt;&lt; term</entry>
   <entry>evaluate when used</entry>
        </row>
        <row>
   <entry>var $+&lt;&lt; term</entry>
   <entry>append, evaluate when used</entry>
        </row>
        <row>
   <entry>var $?&lt;&lt; term</entry>
   <entry>only when not set, evaluate when used</entry>
        </row>
      </tbody>
    </tgroup>
  </informaltable>
</para>
