From: Dominic Fandrey on
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 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


--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Ed Morton on
On Nov 10, 11:10 am, Dominic Fandrey <kamik...(a)bsdforen.de> 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.

Then it's object-based, not object-oriented.

> 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.

Maybe you're trying to do too much in shell if the design paradigm
makes such a big difference!

Ed.
From: Dominic Fandrey on
Ed Morton wrote:
> On Nov 10, 11:10 am, Dominic Fandrey <kamikaze(a)bsdforen.de> wrote:
>> 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.
>
> Maybe you're trying to do too much in shell if the design paradigm
> makes such a big difference!

No dependencies outside the base system. The one rule I must not
break.

Regards

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Ed Morton on
On Nov 10, 2:25 pm, Dominic Fandrey <kamik...(a)bsdforen.de> wrote:
> Ed Morton wrote:
> > On Nov 10, 11:10 am, Dominic Fandrey <kamik...(a)bsdforen.de> wrote:
> >> 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.
>
> > Maybe you're trying to do too much in shell if the design paradigm
> > makes such a big difference!
>
> No dependencies outside the base system. The one rule I must not
> break.

Doesn't creating an object-based middleware framework that has to be
implemented on your base contradict that requirement? I mean, what's
the difference between doing that and just installing Java or similar?
Either way you're introducing something that's potentially beyond the
realm of expertise of anyone who knows the "base" and has to work on
your code in future, but at least they might know Java already, and
there's probably already a version of Java that'll run on whatever
your next "base" is other than freeBSD if it's portability you care
about.

Ed.
From: Janis Papanagnou on
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
>
>