• GAWK Namespace Uses by Example

    From J Naman@21:1/5 to All on Wed Feb 24 14:38:03 2021
    GAWK Namespace Uses by Example
    I don't know if this discussion group is the appropriate place to report
    by example some GAWK only features in more detail than I found elsewhere.
    NOT ANY BUGS. Just useful (to me) example code.
    If there are more appropriate places, blog, etc., for this type of thing, please let me know.
    BEGIN{
    # Use @ns variables for documentation, e.g. constant::pi; maxvalue:foo; minvalue:foo
    # Demo assign @namespace VARiables WITHOUT FORMAL @namespace DECLARATION printf("constant::pi=%s\n",constant::pi=3.14159); #=> constant::pi=3.14159 (success)
    printf("foo::test=%s\n",foo::test=1); #=> foo::test=1 (success) printf(""foo::test=%s\n",++foo::test); #=> foo::test=2 (success)

    # Test declare @namespace FUNCtions without formal @namespace declaration xyz::foo(); #=> xyz:foo called w/out formal @namespace declaration # successful! (see after BEGIN{} ends)

    # Note: ns::names are roughly analogous to a pathname, ns\name
    # Note! except only one level deep (only one ::) per Eff AWK Pgming Guide

    # silly observation; not recommended style
    foo::foo("direct call"); #=> foo:foo called: direct call (success)

    # test @ns INDIRECT FUNCTION CALLS
    indir1="foo::foo"
    @indir1("@indir1 indirect call") #=> foo:foo called: @indir1 indirect call
    # (success)

    ns::indir2="foo::foo"
    @ns::indir2("@ns::indir2 indirect call") #=> foo:foo called: @ns::indir2 indirect call
    # (success) I was almost surprised it worked so nicely ...

    # Test two levels deep foo::moo::debug just to be sure ...
    #x printf("foo::moo::debug=%s\n",foo::moo::debug=2); # FAILS!
    #x gawk: C:\Gtest\nsmespsce.awk:12: error: identifier `foo::moo::debug': namespace separator can only appear once in a qualified name
    # Use: don't
    exit;
    }
    # NOTE: @namespace "xyz" NOT declared, still in awk @ns
    # test @ns FUNCtion declaration WITHOUT FORMAL @namespace DECLARATION (from within awk @ns)
    function xyz::foo(){ printf("#=> xyz:foo called w/out formal @namespace declaration\n");}
    # call from BEGIN was successful!

    # NOTE: FORMAL @namespace DECLARATION example (don't use foo::foo for real ...) @namespace "foo"
    #! foo::foo(note); #=> foo:foo called (success)
    function foo(note){ printf("#=> foo:foo called: %s\n",note);} # successful! #----------

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)