In awk, when is a function definition executed?

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
In awk, when is a function definition executed?
Is a function definition executed even before BEGIN ... is executed? Thanks.
awk
add a comment |
up vote
0
down vote
favorite
In awk, when is a function definition executed?
Is a function definition executed even before BEGIN ... is executed? Thanks.
awk
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In awk, when is a function definition executed?
Is a function definition executed even before BEGIN ... is executed? Thanks.
awk
In awk, when is a function definition executed?
Is a function definition executed even before BEGIN ... is executed? Thanks.
awk
awk
asked yesterday
Tim
24.9k70239434
24.9k70239434
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:
$ cat go.awk
BEGIN
print "The BEGIN block is executing"
print reverse("The BEGIN block is executing")
print reverse($0)
function reverse(str)
trs=""
for(i=length(str); i > 0; i--)
trs=trs substr(str, i, 1);
return trs
And with:
$ cat input
line one
line two
line three
... the result is:
$ awk -f go.awk < input
The BEGIN block is executing
gnitucexe si kcolb NIGEB ehT
eno enil
owt enil
eerht enil
You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.
To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.
The POSIX specification for awk says, most pertinently:
A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.
The gawk man page implies something similar:
... Gawk reads the program text as if all the program-files and com‐
mand line source texts had been concatenated together. This is useful for
building libraries of AWK functions, without having to include them in each
new AWK program that uses them. It also provides the ability to mix library
functions with command line programs.
...
Gawk executes AWK programs in the following order. First, all variable
assignments specified via the -v option are performed. Next, gawk compiles
the program into an internal form. Then, gawk executes the code in the BEGIN
rule(s) (if any), and then proceeds ...
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:
$ cat go.awk
BEGIN
print "The BEGIN block is executing"
print reverse("The BEGIN block is executing")
print reverse($0)
function reverse(str)
trs=""
for(i=length(str); i > 0; i--)
trs=trs substr(str, i, 1);
return trs
And with:
$ cat input
line one
line two
line three
... the result is:
$ awk -f go.awk < input
The BEGIN block is executing
gnitucexe si kcolb NIGEB ehT
eno enil
owt enil
eerht enil
You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.
To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.
The POSIX specification for awk says, most pertinently:
A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.
The gawk man page implies something similar:
... Gawk reads the program text as if all the program-files and com‐
mand line source texts had been concatenated together. This is useful for
building libraries of AWK functions, without having to include them in each
new AWK program that uses them. It also provides the ability to mix library
functions with command line programs.
...
Gawk executes AWK programs in the following order. First, all variable
assignments specified via the -v option are performed. Next, gawk compiles
the program into an internal form. Then, gawk executes the code in the BEGIN
rule(s) (if any), and then proceeds ...
add a comment |
up vote
1
down vote
This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:
$ cat go.awk
BEGIN
print "The BEGIN block is executing"
print reverse("The BEGIN block is executing")
print reverse($0)
function reverse(str)
trs=""
for(i=length(str); i > 0; i--)
trs=trs substr(str, i, 1);
return trs
And with:
$ cat input
line one
line two
line three
... the result is:
$ awk -f go.awk < input
The BEGIN block is executing
gnitucexe si kcolb NIGEB ehT
eno enil
owt enil
eerht enil
You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.
To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.
The POSIX specification for awk says, most pertinently:
A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.
The gawk man page implies something similar:
... Gawk reads the program text as if all the program-files and com‐
mand line source texts had been concatenated together. This is useful for
building libraries of AWK functions, without having to include them in each
new AWK program that uses them. It also provides the ability to mix library
functions with command line programs.
...
Gawk executes AWK programs in the following order. First, all variable
assignments specified via the -v option are performed. Next, gawk compiles
the program into an internal form. Then, gawk executes the code in the BEGIN
rule(s) (if any), and then proceeds ...
add a comment |
up vote
1
down vote
up vote
1
down vote
This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:
$ cat go.awk
BEGIN
print "The BEGIN block is executing"
print reverse("The BEGIN block is executing")
print reverse($0)
function reverse(str)
trs=""
for(i=length(str); i > 0; i--)
trs=trs substr(str, i, 1);
return trs
And with:
$ cat input
line one
line two
line three
... the result is:
$ awk -f go.awk < input
The BEGIN block is executing
gnitucexe si kcolb NIGEB ehT
eno enil
owt enil
eerht enil
You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.
To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.
The POSIX specification for awk says, most pertinently:
A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.
The gawk man page implies something similar:
... Gawk reads the program text as if all the program-files and com‐
mand line source texts had been concatenated together. This is useful for
building libraries of AWK functions, without having to include them in each
new AWK program that uses them. It also provides the ability to mix library
functions with command line programs.
...
Gawk executes AWK programs in the following order. First, all variable
assignments specified via the -v option are performed. Next, gawk compiles
the program into an internal form. Then, gawk executes the code in the BEGIN
rule(s) (if any), and then proceeds ...
This is easily determined; I got curious and decided to test the "worst-case" scenario first, by defining the function at the end of the awk scriptfile:
$ cat go.awk
BEGIN
print "The BEGIN block is executing"
print reverse("The BEGIN block is executing")
print reverse($0)
function reverse(str)
trs=""
for(i=length(str); i > 0; i--)
trs=trs substr(str, i, 1);
return trs
And with:
$ cat input
line one
line two
line three
... the result is:
$ awk -f go.awk < input
The BEGIN block is executing
gnitucexe si kcolb NIGEB ehT
eno enil
owt enil
eerht enil
You can see that the function was defined (the function "definition" was "executed") before the BEGIN block was entered, since the BEGIN block successfully called my user-defined reverse function.
To see that the function is not executed while it's being defined, simply insert a print statement inside the function and observe that there is no output from that statement until the function is called.
The POSIX specification for awk says, most pertinently:
A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global.
The gawk man page implies something similar:
... Gawk reads the program text as if all the program-files and com‐
mand line source texts had been concatenated together. This is useful for
building libraries of AWK functions, without having to include them in each
new AWK program that uses them. It also provides the ability to mix library
functions with command line programs.
...
Gawk executes AWK programs in the following order. First, all variable
assignments specified via the -v option are performed. Next, gawk compiles
the program into an internal form. Then, gawk executes the code in the BEGIN
rule(s) (if any), and then proceeds ...
answered yesterday
Jeff Schaller
35.9k952119
35.9k952119
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481821%2fin-awk-when-is-a-function-definition-executed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown