4 ms·
You have a really good point. AWK feels incredibly modern for being so old.
by kqr 8y ago
You have a really good point. AWK feels incredibly modern for being so old.
- kazinator 8y agoUntil you find that it has no local variables other than function parameters. In a function, local variables can be simulated by defining additional arguments ... and not passing those. Everything that isn't a function parameter is a global! Awk is stupidly confused whether it is a two-namespace or one-namespace language (think Lisp-2 vs. Lisp-1). For instance, this works: function x() { print "x called" } function foo(x) { x() # function x, not parameter x. } but in other situations the two spaces are conflated. For instance sin = 3 is a syntax error because sin is the built-in sinusoid function, even though you're using it as a variable, which shouldn't interfere with function use like sin(3.14).
- coliveira 8y agoActually what you show is that there is a clear rule. First, the symbol is treated as a function. If it is not in the function space, then it is used as a variable name. To make a finer distinction awk would need some form of local variable declaration, which it clearly hasn't.
- kazinator 8y ago> First, the symbol is treated as a function. If it is not in the function space, then it is used as a variable name. That is simply not the case. x() unambiguously treats x as a function, and will fail if there is no such function, even if there is a variable x. $ awk 'function foo(x) { x() } BEGIN { foo(42) }' awk: cmd. line:2: fatal: function `x' not defined > To make a finer distinction awk would need some form of local variable declaration, which it clearly hasn't. Also not the case. The purely syntactic context distinguishes whether or not the identifier is being used as a variable or function. Awk sometimes uses it, sometimes not. This doesn't diagnose: function x() { } function foo(x) { x() # function call allowed x = 3 # assignment allowed } BEGIN { foo(42) } But: function foo(x) { } BEGIN { foo = 3 } fatal: function `foo' called with space between name and `(', or used as a variable or an array Why isn't it a problem that the function x() is used as a variable?
- tannhaeuser 8y agoAwk is basically what JavaScript syntax is based on (no really!). The following (while not terribly useful) is both awk and JavaScript: function f(x, otherArgs) { r = otherArgs["x"] for (v in otherArgs) delete otherArgs[v] return r } JavaScript even uses awk-style regexp literals (though with PCRE semantics, and more features, etc.).