Using AWK for pseudo-OOP?

This is a discussion on Using AWK for pseudo-OOP? within the awk forums in Programming Languages category; Maybe it's silly. Maybe I'm nuts. Who needs another OOP language, right? Since AWK is my language of choice, here are some ideas just for the heck of it. Note: The code is known to run correctly with 'awk version 20070501' and 'GNU Awk 3.1.5' on Mac OS X. If you get errors, check for split comment lines. #**************************************** # # Some code for approximating OOP in AWK # Suggestions and ideas welcome # Jim Hart, jhart@mail.avcnet.org # 3 August 2008 # # provided as-is # no warranty expressed or implied; may contain bugs, may crash your computer # # ...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-04-2008, 07:58 AM
jh
Guest
 
Default Using AWK for pseudo-OOP?

Maybe it's silly. Maybe I'm nuts. Who needs another OOP language, right?
Since AWK is my language of choice, here are some ideas just for the
heck of it.

Note: The code is known to run correctly with 'awk version 20070501'
and 'GNU Awk 3.1.5' on Mac OS X. If you get errors, check for split
comment lines.

#****************************************
#
# Some code for approximating OOP in AWK
# Suggestions and ideas welcome
# Jim Hart, jhart@mail.avcnet.org
# 3 August 2008
#
# provided as-is
# no warranty expressed or implied; may contain bugs, may crash your
computer
#
# in the publice domain, copyright disclaimed
#
#
#************** An object-oriented framework for awk/gawk
*******************

# Uses 3 arrays:

# 1- an array that contains all object variables indexed by object and
# variable name, stores any value. needed to maintain values by object, even
# if it is awkward to use (variables[object, variable name])

# 2- an array that connects specific objects to their class, indexed by
# object, stores class name (objects[object])

# 3- an array that contains class variables indexed by class, stores any
# value (classVariables[class name, variable name])


# classes are functions. Parameters are:
# 1 - object
# 2 - method
# 3-x - however many the class needs (currently 9 max)

# See the example functions, below, for how to define and use classes


# Provides the following OOP-like functions:

# myobject = New(class) - creates an object, stores its class in the
objects[] array
# and returns the object; (actually a unique integer,
# the index for objects[] and the value to be passed
as an object
# to the object methods that take one

# Delete(object) - recovers storage used by all object variables
# and removes the entry in the object list (objects[])



# There is no inheritance.
#
# To treat superclass methods and variables as inherited, pass the
subclass object to the superclass.


# 'gawk' has, optionally, a 'switch/case' statement, but it 'falls
through' if 'break'
# statements aren't included, so I recommend if...else for switching among
# methods. Also, that way, the code will run unchanged in AWK. YMMV


#*************************


BEGIN {
me = New("Person")
myId = Person(me,"set","James","Hart","A")
print Person(me,"get")

jdoe = New("Employee")
jdoeId = Person(jdoe,"set","John","Doe") #'superclass' -
Employee object
jdoeEmpId = Employee(jdoe,"set","ILS")
print Person(jdoe,"get")
print Employee(jdoe,"get")
Delete(jdoe)
print Person(jdoe,"get")
print Employee(jdoe,"get")
}

function New(class) {
objid = ++lastObjId
objects[objid] = class
return objid
}

function Delete(objid) {
delete objects[objid]
delete start[objid]
for(i in variables) {
split(i,_objectVariablesDelete,SUBSEP)
if(_objectVariablesDelete[1] == objid )
delete variables[i]
} # with the latest GAWK could change to 'for( (i,j)
in variables)'
}


########## Put your class functions here; see the examples for
structure. ######

## This is an example class
function Person(objid,method,p1,p2,p3,p4,p5,p6,p7,p8,p9) {

class = objects[objid] # the objects[] array stores the class
of each object

# this is like a "start" method...put initialization stuff here
# AWK doesn't require variables to be typed or initialized, so
no need to set them

if(! start[objid]) {
start[objid] = 1
}

if(method == "set") {
variables[objid,"firstName"] = p1
variables[objid,"lastName"] = p2
variables[objid,"MI"] = p3
if(! variables[objid,"pid"])
variables[objid,"pid"] =
++classVariables[objid,"lastId"]
return variables[objid,"pid"]
}

else if(method == "update") {
if(p1 != "")
variables[objid,"firstName"] = p1
if(p2 != "")
variables[objid,"lastName"] = p2
if(p3 != "")
variables[objid,"MI"] = p3
}

else if(method == "get") {
results = variables[objid,"firstName"]
if(variables[objid,"MI"])
results = results " " variables[objid,"MI"] "."
results = results " " variables[objid,"lastName"]
return results
}

}
function Employee(objid,method,p1,p2,p3,p4,p5,p6,p7,p8,p9) {

class = objects[objid]

if(! start[objid]) {
start[objid] = 1
Person(objid,"start")
}

if(method == "set") {
if(p1 != "")
variables[objid,"dept"] = p1
if(! variables[objid,"empId"])
variables[objid,"empId"] =
++classVariables[class,"lastEmpId"]
}
else if(method == "update") {
if(p1 != "")
variables[objid,"dept"] = p1
}
else if(method == "get") {
return variables[objid,"dept"]
}

}

Reply With Quote
  #2  
Old 08-04-2008, 08:32 AM
Janis Papanagnou
Guest
 
Default Re: Using AWK for pseudo-OOP?

jh wrote:
> Maybe it's silly. Maybe I'm nuts. Who needs another OOP language, right?


It's not a bad thought per se.

> Since AWK is my language of choice, here are some ideas just for the
> heck of it.
>
> [...]
>
> # Provides the following OOP-like functions:
>
> # myobject = New(class) - creates an object, stores its class in the
> objects[] array
> # and returns the object; (actually a unique integer,
> # the index for objects[] and the value to be passed
> as an object
> # to the object methods that take one
>
> # Delete(object) - recovers storage used by all object variables
> # and removes the entry in the object list (objects[])
>
>
>
> # There is no inheritance.


Lacking the crucial OO language element; in which way is your
proposal supporting OO at all, then? It isn't, I'd say.

<OT> I once noticed in a newer implementation of the very first
object oriented language, Simula 67, called cim, which despite
supporting inheritance (as defined by the language standard) it
didn't support polymorphism, another crucial OO element. I'd be
reluctant to call that incomplete Simula implementation OO, even
though the cim compiler still suported many OO language concepts
like encapsulation, abtraction, etc. (Analysing the cim parser
showed that it was possible to obtain the polymorphism as a side
effect using some non-standard syntax; the function was actually
implemented just not accessible as consequence of an obscure bug.
Okay I am digressing...) </OT>

A language needs more than a few suporting functions to be OO.

Janis

> #
> # To treat superclass methods and variables as inherited, pass the
> subclass object to the superclass.
>
> [...]

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 09:11 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.