Re: object oriented shell scripts



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


.



Relevant Pages

  • Re: .Net COM+
    ... write your own IUnknown to expose a programmatic interface to ... management leaves a lot to be desired (Reference counts isn't perfect, ... transaction features can be used .. ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: MS Data Access Block
    ... features, I meant in my program that uses the block, not that I made changes ... in project 'Forms1' cannot be copied to the run directory because it would ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: On assembly and portability (between Linux and Windows)
    ... A short note on Object Oriented Programming (OOP). ... Inheritance is the ability to extend data structures by ... Polymorphism is the ability to override an existing ... oriented features. ...
    (alt.lang.asm)
  • Re: object oriented shell scripts
    ... It doesn't provide type safety or inheritance or access control, ... And all the Real OO Features are missing? ... think it's possible to port Bourne Shell code or even write code ...
    (comp.unix.shell)
  • Re: Eiffel, C++ and the Diamond Problem
    ... One of the attractions of Eiffel is the claim ... > oriented languages either avoided multiple inheritance entirely (e.g. ... as if B's features were originally constructed in A. ...
    (comp.lang.eiffel)