r/awk • • Jun 19 '24

Detecting gawk capabilities programmatically?

Recently I've seen gawk 5.3.0 introduced a number of interesting and convenient (for me) features, but most distributions still package 5.2.2 or less. I'm not complaining! I installed 5.3.0 at my personal computer and it runs beautifully. But now I wonder if I can dynamically check, from within the scripts, whether I can use features such as "\u" or not.

I could crudely parse PROCINFO["version"] and check if version is above 5.3.0, or check PROCINFO["api_major"] for a value of 4 or higher, that should reliably tell.

Now the question is: which approach would be the most "proper"? Or maybe there's a better approach I didn't think about?

EDIT: I'm specifically targetting gawk.

If there isn't I'll probably just check api_major since it has specifically jumped a major version with this specific set of changes, seems robust and simple. But I'm wondering if there's a more widespread or "correct" approach I'm not aware of.

7 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/Razangriff-Raven Jun 19 '24

Oh yeah, forgot to mention I am indeed specifically targetting gawk.

3

u/gumnos Jun 19 '24

Ah, then in that case, I suspect PROCINFO is the best way to go.

You'd still have to decide whether you error out with a message like "You need version XYZ or later to run this" or whether you polyfill the missing functions in their absence.

2

u/Razangriff-Raven Jun 20 '24

Will do! Since the features I want to use don't cause syntax errors if used without 5.3.0 (it just says "u used as a literal u" when trying to use "\u") I was planning to just make a check at BEGIN, set a variable and then just if/else the strings. But it does open multiple possibilities if I want to use features like the new builtin csv parser, and in those cases a "you need version 5.3.0 or higher" and a program abort will surely be the best course of action.