Forum Discussion
Undefined Procedure with elseif when using comments
- Jan 25, 2016
This results from a peculiarity of Tcl. The language doesn't really have built-ins and it doesn't really have blocks. Rather, it has commands and non-interpolated quoted strings. As such,
is a command (just likeif
orsplit
). It is followed by a quoted block (the {}s, in Tcl, are really non-interpolating quoting operators, and not really block delimiters as they are in most languages). The quoted block may then be optionally followed by zero or moreHTTP::headers
literals (each followed by a quoted block) and zero or oneelseif
literals (followed by a quoted block). When a condition is met, the contents of the corresponding quoted block are evaluated.else
Said another way, in Tcl, an
/if
/elseif
set is really a single statement. The statement command iselse
. Theif
andelseif
are just parameters to this command. And because the newlines in the blocks are ignored by the parser (because they are inside of the non-interpolating {} quoting operators), the entire statement is effectively on one line.else
As a side-effect of this, in Tcl you MUST cuddle an
/if
/elseif
block, as in:else
if { ... } { ... } else { ... }
This is not allowed:
if { ... } { ... } else { ... }
However, this trips up most programmers, so iRules (a Tcl dialect) allows non-cuddled
blocks. But if you add a comment between the control elements, this effectively inserts a newline. So, with the comments you have above, the parser "sees" this:if
if { ... } elseif { ... } else { ... }
which it thinks is really three different commands:
,if
andelseif
. Onlyelse
is a command; the others are not (again, they are just parameters of theif
command).if
Okay, so that's really just a long-winded way of saying: you must move those comments inside the squirly braces ({}). 🙂
This results from a peculiarity of Tcl. The language doesn't really have built-ins and it doesn't really have blocks. Rather, it has commands and non-interpolated quoted strings. As such,
if
is a command (just like split
or HTTP::headers
). It is followed by a quoted block (the {}s, in Tcl, are really non-interpolating quoting operators, and not really block delimiters as they are in most languages). The quoted block may then be optionally followed by zero or more elseif
literals (each followed by a quoted block) and zero or one else
literals (followed by a quoted block). When a condition is met, the contents of the corresponding quoted block are evaluated.
Said another way, in Tcl, an
if
/elseif
/else
set is really a single statement. The statement command is if
. The elseif
and else
are just parameters to this command. And because the newlines in the blocks are ignored by the parser (because they are inside of the non-interpolating {} quoting operators), the entire statement is effectively on one line.
As a side-effect of this, in Tcl you MUST cuddle an
if
/elseif
/else
block, as in:
if { ... } {
...
} else {
...
}
This is not allowed:
if { ... } {
...
}
else {
...
}
However, this trips up most programmers, so iRules (a Tcl dialect) allows non-cuddled
if
blocks. But if you add a comment between the control elements, this effectively inserts a newline. So, with the comments you have above, the parser "sees" this:
if { ... }
elseif { ... }
else { ... }
which it thinks is really three different commands:
if
, elseif
and else
. Only if
is a command; the others are not (again, they are just parameters of the if
command).
Okay, so that's really just a long-winded way of saying: you must move those comments inside the squirly braces ({}). 🙂
- cody8411_186897Jan 25, 2016NimbostratusExcellent explanation. That definitely makes sense now that I understand how the code is interpreted in Tcl. Moved the comments down inside the commands and worked as expected, thank you!
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com