manpagez: man pages & more
info make
Home | html | info | man

File: make.info,  Node: Let Function,  Next: Foreach Function,  Prev: Functions.php">Conditional Functions,  Up: Functions

8.5 The 'let' Function
======================

The 'let' function provides a means to limit the scope of a variable.
The assignment of the named variables in a 'let' expression is in effect
only within the text provided by the 'let' expression, and this
assignment doesn't impact that named variable in any outer scope.

   Additionally, the 'let' function enables list unpacking by assigning
all unassigned values to the last named variable.

   The syntax of the 'let' function is:

     $(let VAR [VAR ...],[LIST],TEXT)

The first two arguments, VAR and LIST, are expanded before anything else
is done; note that the last argument, TEXT, is *not* expanded at the
same time.  Next, each word of the expanded value of LIST is bound to
each of the variable names, VAR, in turn, with the final variable name
being bound to the remainder of the expanded LIST.  In other words, the
first word of LIST is bound to the first variable VAR, the second word
to the second variable VAR, and so on.

   If there are more variable names in VAR than there are words in LIST,
the remaining VAR variable names are set to the empty string.  If there
are fewer VARs than words in LIST then the last VAR is set to all
remaining words in LIST.

   The variables in VAR are assigned as simply-expanded variables during
the execution of 'let'.  *Note The Two Flavors of Variables: Flavors.

   After all variables are thus bound, TEXT is expanded to provide the
result of the 'let' function.

   For example, this macro reverses the order of the words in the list
that it is given as its first argument:

     reverse = $(let first rest,$1,\
                 $(if $(rest),$(call reverse,$(rest)) )$(first))

     all: ; @echo $(call reverse,d c b a)

will print 'a b c d'.  When first called, 'let' will expand $1 to 'd c b
a'.  It will then assign FIRST to 'd' and assign REST to 'c b a'.  It
will then expand the if-statement, where '$(rest)' is not empty so we
recursively invoke the REVERSE function with the value of REST which is
now 'c b a'.  The recursive invocation of 'let' assigns FIRST to 'c' and
REST to 'b a'.  The recursion continues until 'let' is called with just
a single value, 'a'.  Here FIRST is 'a' and REST is empty, so we do not
recurse but simply expand '$(first)' to 'a' and return, which adds ' b',
etc.

   After the REVERSE call is complete, the FIRST and REST variables are
no longer set.  If variables by those names existed beforehand, they are
not affected by the expansion of the 'reverse' macro.

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.