ALERT! The community will be read-only starting on April 19, 8am Pacific as the migration begins. Read more for important details.
ALERT! The community will be read-only starting on April 19, 8am Pacific as the migration begins.Read more for important details.

SBM MODSCRIPT, PART 15 - Singletons

SBM MODSCRIPT, PART 15 - Singletons

As a common programming pattern, singletons are very helpful with data that needs to be read once and then reused throughout the program. This may not be required for simple scripts, but for long running, complicated scripts, this can help with keeping the script running with high performance. Since scripts often run as parts of transitions that users are clicking through, performance can affect user confidence and contentment.

To create a singleton in ModScript, follow the following pattern:

def GetConfiguration() {
  global g_appconfiguration;
  if ( g_appconfiguration.is_var_undef() ) {
    g_appconfiguration = Map();
    // read various configuration settings from DB or file system and inject into the map
  return g_appconfiguration;

Create a function. Inside the function, declare a global (but do not initialize it). Check if the global is undef, if so, initialize it and set it up. Finally, return the global. The global variable should have a very unique name, as it is global and we do not want it to interfere with other scoped variables. Since it is global, this means it will have the lifetime of the script, meaning the check for is_var_undef() will only be true the first time. As such, you can be confident that the time taken to read configuration data will only occur once, and can be accessed as often as needed throughout the script lifetime. 


SBM ModScript - Table of Contents

Labels (1)


Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
3 of 3
Last update:
‎2020-07-29 16:09
Updated by:
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.