Studio:Callback functions in Perl scripts: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
m (Text replace - 'Category:Perl Info' to 'Category:Studio:Perl Info') |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
The following example demonstrates a user callback registration service with an owner callback function: | The following example demonstrates a user callback registration service with an owner callback function: | ||
<source lang="perl"> | |||
# The following example illustrates how to script a function pointer | |||
# that has been qualified with the scl_ptr_flist pragma. | |||
# Perspective: User of the regCallBack interface, Owner of the callBack interface | |||
# Standard Perl setup | |||
use strict; | |||
use Carp; | |||
use Win32::OLE; | |||
Win32::OLE->Option(Warn => 3); | |||
# Create a reference (shortcut) to the User instance of the regCallBack function | |||
my $uRegCallBack = $main::ascript->Functions->Item("regCallBack")->User; | |||
# Create a reference (shortcut) to the Owner instance of the regCallBack function | |||
my $oCallBack = $main::ascript->Functions->Item("callBack")->Owner; | |||
# Register for ownership of the callback function | |||
$oCallBack->Register(); | |||
# Assign the callback function to the registration function's parameter cb | |||
$uRegCallBack->ParameterList->LetProperty("cb", $oCallBack); | |||
# Call the registration function passing in the callback function | |||
$uRegCallBack->Call(); | |||
# Wait for the callback function to be called | |||
my $event = $main::ascript->WaitForEvent(); | |||
# If the received event was the callback function | |||
if ($event->Name == $oCallBack->Name) { | |||
# Process the function call | |||
# return the callback function to the user | |||
$oCallBack->Return(); | |||
} | |||
</source> | |||
[[Category: | [[Category:Studio:Perl Info]] |
Latest revision as of 23:52, 20 August 2009
To use callback functions in Perl scripts, use LetProperty(). This is an ActiveState Perl requirement for assigning a COM object (e.g., another interface) to a read/write property.
The following example demonstrates a user callback registration service with an owner callback function:
# The following example illustrates how to script a function pointer
# that has been qualified with the scl_ptr_flist pragma.
# Perspective: User of the regCallBack interface, Owner of the callBack interface
# Standard Perl setup
use strict;
use Carp;
use Win32::OLE;
Win32::OLE->Option(Warn => 3);
# Create a reference (shortcut) to the User instance of the regCallBack function
my $uRegCallBack = $main::ascript->Functions->Item("regCallBack")->User;
# Create a reference (shortcut) to the Owner instance of the regCallBack function
my $oCallBack = $main::ascript->Functions->Item("callBack")->Owner;
# Register for ownership of the callback function
$oCallBack->Register();
# Assign the callback function to the registration function's parameter cb
$uRegCallBack->ParameterList->LetProperty("cb", $oCallBack);
# Call the registration function passing in the callback function
$uRegCallBack->Call();
# Wait for the callback function to be called
my $event = $main::ascript->WaitForEvent();
# If the received event was the callback function
if ($event->Name == $oCallBack->Name) {
# Process the function call
# return the callback function to the user
$oCallBack->Return();
}