Re: object oriented shell scripts
- From: Janis Papanagnou <janis_papanagnou@xxxxxxxxxxx>
- Date: Wed, 11 Nov 2009 00:23:03 +0100
Dominic Fandrey wrote:
I have written a framework for object oriented shell scripting.
It doesn't provide type safety or inheritance or access control,
but it has classes, objects and some other nice features like
automatic creation of getters and setters or return by reference.
I think the getter and setter is the most unimportant feature in an
object based approach. And all the Real OO Features are missing?
BTW, you may want to have a look into a newer ksh93 and its typeset
builtin command with its options...
[Option -n]
Declares vname to be a reference to the variable whose name
is defined by the value of variable vname. This is usually
used to reference a variable inside a function whose name
has been passed as an argument.
[Option -T]
With the -T option of typeset, the type name, specified as
an option argument to -T, is set with a compound variable
assignment that defines the type. Function definitions can
appear inside the compound variable assignment and these
become discipline functions for this type and can be invoked
or redefined by each instance of the type. The function name
create is treated specially. It is invoked for each instance
of the type that is created but is not inherited and cannot
be redefined for each instance.
And see the manual about the very interesting discipline functions.
....in case you want to use one of the newer standard shells out there.
(Don't know of any more detailled documentation or tutorial, though.)
Janis
.
I am working on a rather complex application and the cleaner
data structures safe enough computation time to outweigh the
considerable overhead caused by the framework.
If you're interested in using it or porting it to the /bin/sh of
a different operating system (currently runs on FreeBSD), feel
free to contact me.
The following is some working demo code that illustrates how
it is used. It outputs the fibonacci numbers 8 and 7:
21
13
#!/bin/sh
#
# A small demo of "bsda_obj.sh", which demonstrates
# return by reference. Note that this is even works
# safely when the variables within a method have the
# same names as the variables in the caller context
# (such as is the case for recursive methods).
#
# These features are really just useful byproducts
# of my desire to write object oriented shell
# scripts.
#
# Import framework.
. bsda_obj.sh
# Declare the class.
bsda_obj:createClass Demo \
w:value \
This is a comment \
x:fibonacciRecursive \
"This is a comment, too. <== my prefered style" \
#
# Implementation of the fibonacciRecursive method for the
# Demo class.
#
# Yes I know that this is the least efficient way of
# doing this, but it demonstrates what I want it to.
#
# @param 1
# The variable to store the fibonacci value in.
# @param 2
# The index of the fibonacci value to return.
#
Demo.fibonacciRecursive() {
# Terminate recursion.
if [ $2 -le 2 ]; then
$caller.setvar "$1" 1
return 0
fi
local f1 f2
$this.fibonacciRecursive f1 $(($2 - 1))
$this.fibonacciRecursive f2 $(($2 - 2))
$caller.setvar "$1" $(($f1 + $f2))
}
# Create instance.
Demo demo
# Call the fibonacci method from instance and ...
# ... store the result in the value variable.
$demo.fibonacciRecursive value 8
# ... print the result.
$demo.fibonacciRecursive '' 8
# Set an attribute.
$demo.setValue $(($value - $($demo.fibonacciRecursive '' 6)))
# Get an attribute and ...
# ... store the result in the value variable.
$demo.getValue value
# ... print the attribute.
$demo.getValue
- Follow-Ups:
- Re: object oriented shell scripts
- From: Dominic Fandrey
- Re: object oriented shell scripts
- References:
- object oriented shell scripts
- From: Dominic Fandrey
- object oriented shell scripts
- Prev by Date: Re: object oriented shell scripts
- Next by Date: Re: object oriented shell scripts
- Previous by thread: Re: object oriented shell scripts
- Next by thread: Re: object oriented shell scripts
- Index(es):
Relevant Pages
|