bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-gawk] Gawk 4.2.0 Unexpected handling of function's input variables


From: Finn Magnusson
Subject: [bug-gawk] Gawk 4.2.0 Unexpected handling of function's input variables
Date: Mon, 27 Nov 2017 19:22:48 +0000 (UTC)

Dear Gawk Support

I noticed a change in the way that gawk 4.2.0 treats the variables declared as input parameter to a function (compared to previous gawk versions).

See the below code, please put it in a file x.awk and execute with "gawk -f x.awk"

{ test($1)}
function test(var)
{
    print 1,var
    system("echo x > dummyfile")
    while (getline < "dummyfile"){}
    close("dummyfile")
    system("rm dummyfile")
    print 2,var
}


When executing this code it gives for instance when typing "a" on stdin:
1 a
2 x

So the variable "var" is getting changed from its initial value "a" to the new value "x".
That is because when the function "test" read the file "dummyfile" that contained the letter "x" then the variable $1 is given the value "x" and then this gets reflected into the variable var. So I wonder how come a change of "$1" is affecting "var", should "var" not be local to the "test" function?

Note that in all previous gawk versions this does not happen, the input variable "var" keeps its value "a" and does not get changed to "x".

Is this a bug or is it expected behaviour?

If it is expected behaviour, is there a way I can modify my code to prevent this from happening?

Thank you
Finn Magnusson



reply via email to

[Prev in Thread] Current Thread [Next in Thread]