Hi!

----

[Per offlist request for a "typeset -m" demo and to answer the
question how to abort object creation _after_ it has been created...]

The following demo shows how to pass variables/objects via
namerefs+typeset -m (move variables) between shell functions... in
this demo the function "createfish_t" creates a function-local (=in
the functions local scope) variable of type "printfish_t" and then
validates whether the object represents a "real" fish... if "yes" it
returns the object via nameref (which stores it as a member of the
compound variable "c" of the calling "main" function) ... otherwise
the object remains in the scope of function "createfish_t" and is
destroyed when the function exists.
-- snip --
typeset -T printfish_t=(
        typeset fname

        function unset
        {
                printf '# fish %q died.\n' "${_.fname}"
        }
)

function createfish_t
{
        nameref ret=$1
        typeset fishname="$2"

        printfish_t f
        f.fname="$fishname"

        # return the object via nameref _only_ if it
        # is a true fish
        if [[ "${f.fname}" == ~(Ei)(coelacanth|trout) ]] ; then
                typeset -m 'ret=f'
        fi

        return 0
}

function main
{
        compound c

        print '# creating objects...'
        createfish_t c.shark 'coelacanth'
        createfish_t c.horse 'horse'
        createfish_t c.trout 'trout'
        print '# ... objects created.'

        print -v c
}

# program start.
set -o nounset

print '# calling main...'
main
print '# main done.'
-- snip --

Output should look like this (ast-ksh.2013-04-22 prints this CORRECT :-) ):
-- snip --
# calling main...
# creating objects...
# fish horse died.
# ... objects created.
(
        printfish_t shark=(
                fname=coelacanth
        )
        printfish_t trout=(
                fname=trout
        )
)
# fish coelacanth died.
# fish trout died.
# main done.
-- snip --

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) [email protected]
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to