|
APGen Developer's Guide |
|
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.
When a single script language is used for an APG script and all of its include files, execution occurs as you would expect:
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:
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.
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.