APGen Documentation Previous Topic: Using Other Script Languages Next Topic: Web Site Optimization Techniques Parent Topic: APGen Developer's Guide    APGen Developer's Guide
Mixing Script Languages
See Also:

Multiple script languages can be used in an APG script and its include files.  In addition, function calls and variable references can be made between different script languages.  When mixing script languages, you should be aware that the order of execution of global script may not occur how you intended.  This topic discusses order of execution issues and how to get around them.

Order of Script Execution

When a single script language is used for an APG script and all of its include files, execution occurs as you would expect:

  1. Global script and content is executed top to bottom.  Any function calls from global scope are executed as part of this sequence.
  2. After global script and content are finished executing, a function named Main(), if it exists, is executed.

Global script is script that is not included in a function or procedure.  Global content is content that is not included in a function or procedure.

When multiple script languages are used in an APG script and all of its include files, execution order of global script and content is less intuitive.  These are the guidelines for how execution occurs when script languages are mixed:

  1. Execution of all global script for a given script language is executed top to bottom.
  2. The last script engine instantiated in the script gets its global script executed first.  The first script engine used in the script gets its global script and content executed last.  This is consistent with the order of execution within ASP.  This rule may change; it's not a good idea to use this knowledge when writing an APG script.
  3. The default script language is used to execute all content blocks.  Since the default script language is instantiated first, global script and content using the default script language is executed last.
  4. After all global script and content in all script languages is executed, a function named Main(), if it exists, is executed.  There should only be one Main() function per script plus include files.

This order of execution can be confusing, and relying on it can make code fragile.  We recommend against using global code or content in mixed-language APG scripts.

Best Practices for Mixed Languages

The goal with these "Best Practices" is to eliminate dependency on the order of execution of global code.  If these practices are followed, you should have no problems mixing multiple script languages.  Note that these rules are only necessary when mixing script languages:

Here is an example APG script that mixes JScript, PerlScript, Python, and VBScript.  Note that there is no global code or content - all code and content are placed in subroutines:

<%# @Language="JScript" #%>
<SCRIPT RUNAT=APGen LANGUAGE=PerlScript>


sub PrintDate
     {    
     ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();
     $thisday=(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)[$wday];
     $thismon=(January,February,March,April,May,June,July,August,September,October,November,December)[$mon];
     $ampm = 'AM';
     if ($hour > 12) {
          $hour-=12;
          $ampm = 'PM';
          }
     $time = sprintf '%d:%2.2d:%2.2d',$hour,$min,$sec;
     $year += 1900;
     $datetime = $time.' '.$ampm.', '.$thisday.', '.$thismon.' '.$mday.', '.$year;
     $Output->Write("This web page was created at ");
     $Output->Write($datetime);
     $Output->Write(".<BR>\r\n");
     }

sub Main
     {
     ## call the VBScript GetName function
     $name = $ScriptingNamespace->GetName();

     ## call the JScript CreatePersonalWebPage function
     $ScriptingNamespace->CreatePersonalWebPage($name);
     }

</SCRIPT>
<SCRIPT RUNAT=APGen LANGUAGE=Python>


import string

# This function uses the Python language
def OutputLastName(suName):
     sName = str(suName) # suName is Unicode, sName is a Python string
     nLastSpace = string.rfind(sName, " ")
     if (nLastSpace < 0):
          Output.Write("{Last name not found}")
     else:
          sLastName = sName[nLastSpace+1:]
          Output.Write(sLastName)

</SCRIPT>
<SCRIPT LANGUAGE=VBScript RUNAT=APGen>


Function GetName
     GetName = InputBox("Enter your name:")
End Function

</SCRIPT>
<%#
// This is JScript, the default script language

function CreatePersonalWebPage(sName)
     {
     Output.Filename = sName + ".htm";
#%><HTML><BODY>
     <H3>Hello
<%# =sName #%>, this is your personal web page.</H3>
<%#      
     // Call PerlScript PrintDate function
     PrintDate();
    
     // Call Python OutputLastName function
#%> <P>Your last name is: <%# OutputLastName(sName) #%></P>
     </BODY></HTML>
<%#  }

#%>

Using this technique, there is no dependency on the order of execution of global code.

If you must have global content or script, adhere to these rules:

This relies on the fact that global code in the default script language is executed last.  By removing global content or script in the secondary languages, there is less dependency on the order of execution of global code.