administration mode
Pssst...Ferdy is the creator of JungleDragon, an awesome wildlife community. Visit JungleDragon

 

Article: Dynamic Lotusscript »

FERDY CHRISTANT - FEB 11, 2005 (06:18:34 PM)

Introduction

This short article gives an introduction to the underrated Execute method that is available in Lotusscript. The basics are explained through some simple examples, after which two suggestions are made for advanced usage.

The basics

The Execute method in Lotusscript is an underused feature that can be extremely powerful when used properly. This method allows you to dynamically load, execute, and unload Lotusscript modules. The syntax is extremely simple:

Execute ( text )

Where text is a string containing the script to execute. Here's a simple example:

Sub Initialize
   Dim strCode As String
   strCode = | msgbox "I'm dynamic!" |
   Execute( strCode )
End Sub

When you run this code, it will display a message box that says "I'm dynamic!". Now let's have a look at a dynamic script that spans multiple lines:

Sub Initialize
   Dim strCode As String
   strCode = |
   Dim strMessage as String
   strMessage = "I'm dynamic"
   msgbox strMessage
    |
   Execute( strCode )
End Sub

Note that pipeline characters are used to span multiple lines of Lotusscript into the strCode variable. This second example has exactly the same effect as the first, yet this time the dynamic code spans multiple lines and the message to prompt is defined in a variable.

The essence of the Execute method should be clear by now; one can dynamically run Lotusscript at runtime. The next two paragraphs demonstrate two interesting applications of this feature.

Dynamic class loading

Besides the simple series of statements from the previous examples, one can also include the Use and Declare keywords in the dynamic script. Consider the following script:

Sub Initialize
   Dim strCode as String
   strCode = |
   Use "libArrayManager"
   Dim AM as ArrayManager
   Set AM = new ArrayManager()
   |
   Execute strCode
End Sub

This dynamic code loads a custom fictional class ArrayManager and instantiates a new ArrayManager object from it. The point in this example is that in line 4 we are dynamically loading a module. Thus, at runtime, we can programatically decide to load or not load a module. This technique is called Dynamic Class Loading. When applied correctly this technique can lead to performance gains, because only modules that are needed are actually loaded.

This is not a new technique, I have been using it successfully for years. The first time it appeared was in the must-read redbook Performance Considerations for Domino Applications. Bill Buchan has presented it at Lotussphere '05 as well, so I'll refer you to those two sources for the full details.

Web services

The second advanced application of the Execute method comes quite close to Web Services. One of the key concepts of a web service is that you can dynamically call functions (services) and retrieve the return values, if any. The other concept is that you can do this from anywhere, independant of your calling client, OS or hardware.

Allow me to demonstrate how you can realize the first concept through a simple example:

Declarations
   Dim strReturnVal as String
Sub Initialize
   Dim strCode as String
   Dim strModule as String
   Dim strFunction as String
   Dim strParam as String

   strModule = "libUser"
   strFunction = "getCountry"
   strParam = "Orky Dorky"

   strCode = |
   Use | & strModule & |
   strReturnVal = | & strFunction & |(| & strParam & |)|
   Execute strCode
   Msgbox strReturnVal
End Sub

Look at that. Everything is dynamic, the module to load, the function to execute, and the parameter to pass on. Pay particular attention to the bold line in the example. Key concept here is that we can communicate between our static and dynamic script through variables. Note that this only works for globally declared variables, that's why strReturnVal is declared in the top line of the example.

So how does this come close to a web service? We have proven that we can call a function and pass parameters based on data. In the example, this data is defined in variables, but they might have been coming from a SOAP message, email, or Notes document.

Of course, the example is too simple. Not all functions have a return value. Not all functions have exactly one parameter of that data type. Just think of the base concept, the road for web services is paved. Available from Notes 4.6, go figure!

Share |

Comments: 9
Reviews: 4
Average rating: rating
Highest rating: 5
Lowest rating: 4

COMMENT: SPUG rating

FEB 14, 00:57:59

comment » Good Article .... but "Orky Dorky" ? Is that a coding standard ; -) «

COMMENT: INGO BEYER emailrating

FEB 14, 11:42:20

comment » Nice,

but you have a little error in your last example.

Msgbox strVal should be Msgbox strReturnVal, or?

Good idea

Ingo «

COMMENT: FERDY

FEB 14, 18:50:38

comment » Spug, thanks...but never mess again with my father's name

Ingo, thank you, got that fixed now. «

COMMENT: DON email

FEB 17, 07:48:30

comment » Good article Ferdy,

problem with building web services using Execute is that it won't hop over database or even server barriers easily. I was thinking of building a LS or LS2J library and accessing it from other databases or even servers.

But maybe I'm wrong and someone has a good approach on this. «

COMMENT: DON emailrating

FEB 17, 10:13:37

comment » Maybe a good approach would be combining it with the XMLHttpRequest javascript object. A good introduction can be found at http://www.xml.com/pub/a/2005/02/09/xml-http-request.html «

COMMENT: ANDREI KOUVCHINNIKOV homepage

JUL 4, 11:46:03

comment » I wonder if Execute can cause a higher "crashability" of code, specially when used in a loop and with LS2J libraries. «

COMMENT: TOM

NOV 13, 13:47:43

comment » How to do that with "If ... else" ?? «

COMMENT: PRASOB

DEC 8, 2008 - 10:00:28 AM

comment » Excellent .Really usefull «

COMMENT: TOM rating

FEB 1, 2012 - 02:04:14

comment » Good Article! Everything is dynamic, as you said «

RATE THIS CONTENT (OPTIONAL)
Was this document useful to you?
 
rating Awesome
rating Good
rating Average
rating Poor
rating Useless
CREATE A NEW COMMENT
required field
required field HTML is not allowed. Hyperlinks will automatically be converted.