Studio:Callback functions in Perl scripts: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
No edit summary
m (Text replace - 'Category:Perl Info' to 'Category:Studio:Perl Info')
 
(2 intermediate revisions by the same user 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:
<tt># The following example illustrates how to script a function pointer<br>
<source lang="perl">
# that has been qualified with the scl_ptr_flist pragma. <br>
# The following example illustrates how to script a function pointer
# Perspective: User of the regCallBack interface, Owner of the callBack interface<br>
# that has been qualified with the scl_ptr_flist pragma.  
# Standard Perl setup<br>
# Perspective: User of the regCallBack interface, Owner of the callBack interface
use strict;<br>
# Standard Perl setup
use Carp;<br>
use strict;
use Win32::OLE;<br>
use Carp;
Win32::OLE->Option(Warn => 3);<br><br>
use Win32::OLE;
# Create a reference (shortcut) to the User instance of the regCallBack function<br>
Win32::OLE->Option(Warn => 3);
my $uRegCallBack = $main::ascript->Functions->Item("regCallBack")->User;<br><br>
# Create a reference (shortcut) to the User instance of the regCallBack function
# Create a reference (shortcut) to the Owner instance of the regCallBack function<br>
my $uRegCallBack = $main::ascript->Functions->Item("regCallBack")->User;
my $oCallBack = $main::ascript->Functions->Item("callBack")->Owner;<br><br>
# Create a reference (shortcut) to the Owner instance of the regCallBack function
# Register for ownership of the callback function<br>
my $oCallBack = $main::ascript->Functions->Item("callBack")->Owner;
$oCallBack->Register();<br><br>
# Register for ownership of the callback function
# Assign the callback function to the registration function's parameter cb<br>
$oCallBack->Register();
$uRegCallBack->ParameterList->LetProperty("cb", $oCallBack);<br><br>
# Assign the callback function to the registration function's parameter cb
# Call the registration function passing in the callback function<br>
$uRegCallBack->ParameterList->LetProperty("cb", $oCallBack);
$uRegCallBack->Call();<br><br>
# Call the registration function passing in the callback function
# Wait for the callback function to be called<br>
$uRegCallBack->Call();
my $event = $main::ascript->WaitForEvent();<br><br>
# Wait for the callback function to be called
# If the received event was the callback function<br>
my $event = $main::ascript->WaitForEvent();
if ($event->Name == $oCallBack->Name) {<br>
# If the received event was the callback function
    # Process the function call<br>
if ($event->Name == $oCallBack->Name) {
    # return the callback function to the user<br>
  # Process the function call
    $oCallBack->Return();<br>
  # return the callback function to the user
} <br></tt>
  $oCallBack->Return();
}
</source>






[[Category:Perl Info]]
[[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();
}