Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GAWK Issue/question/curiosity #1: (include file guards)

24 views
Skip to first unread message

Kenny McCormack

unread,
Mar 26, 2015, 9:52:30 AM3/26/15
to
When you use the "include" mechanism - either via the "-i" command line
option or via "@include" in the code - is their protection against the same
file being included twice?

I think there is such protection - but want confirmation.
Also, is it in the docu?

Suppose foo.awk defines a function foo(). If I do:

gawk4 -i foo -i foo 'BEGIN { foo() }'

it works as expected. But note that if I try to define foo() twice in
regular code, I do get an error:

$ gawk4 'function foo() {} function foo() {}'
gawk4: cmd. line:1: error: function name `foo' previously defined
$

P.S. Where this comes up in real life is say you are building a library of
functions, and you want "bar.awk" to start with:

@include "foo"

And you want this to work correctly even if the user @include's foo on his
own (or if he @include's something else that also uses [and @include's] "foo").

--
Religion is what keeps the poor from murdering the rich.

- Napoleon Bonaparte -

Andrew Schorr

unread,
Mar 27, 2015, 4:09:53 PM3/27/15
to
On Thursday, March 26, 2015 at 9:52:30 AM UTC-4, Kenny McCormack wrote:
> When you use the "include" mechanism - either via the "-i" command line
> option or via "@include" in the code - is their protection against the same
> file being included twice?
>
> I think there is such protection - but want confirmation.
> Also, is it in the docu?

Yes and yes.

https://www.gnu.org/software/gawk/manual/html_node/Options.html#Options

-i source-file
--include source-file

Read awk source library from source-file. This option is completely equivalent to using the '@include' directive inside your program. This option is very similar to the -f option, but there are two important differences. First, when -i is used, the program source will not be loaded if it has been previously loaded, whereas the -f will always load the file. Second, because this option is intended to be used with code libraries, gawk does not recognize such files as constituting main program input. Thus, after processing an -i argument, gawk still expects to find the main source code via the -f option or on the command-line.


Kenny McCormack

unread,
Mar 27, 2015, 4:29:28 PM3/27/15
to
In article <22015a47-893a-4b4c...@googlegroups.com>,
Thank you very much!

--
"That's the eternal flame."[7] The single became another worldwide hit.[8]

Hoffs was actually naked when she recorded the song, after being convinced
by Sigerson that Olivia Newton-John got her amazing performances by
recording everything while naked.[9]

(From: http://en.wikipedia.org/wiki/The_Bangles)

0 new messages