Perl and the AR System® Filter API

Version 2
    Share:|

    This week's theme:  Cool Tech Tips
     

    Perl and the AR System® Filter API

    Have you ever wanted to break out of basic AR System development to utilize your favorite scripting language?

    Some scripting languages are well suited to tackle what would otherwise be tedious tasks with very little effort.  Even though AR System enables you to create critical applications without writing code, you may come across tasks where a line or two of powerful scripting code works wonders.  This is where the AR System Filter (ARF) API comes into play.  This Tips & Tricks article highlights the ARF plug-in for Perl, which is available, with the following examples, in Community Downloads in the AR System Developer Community.

    What on Earth Would I do With This Contraption?

    First, some background...  Calls to ARF plug-ins are made through filters.  Whenever an operation is performed on an entry in a form, you can define filters to check or alter the operation.  An operation, in this case, might be the creation or a modification of an entry. One of the compelling leverage points with the ARF plug-ins is the ability to hand off a script to an embedded script engine, have that engine crunch data or access data sources, and return information.  You can then use that information to make decisions or change data in filter workflow.

    Figure 1 (See below) depicts the interaction among the AR System server, the arplugin service, the ARF Perl plug-in, and the Perl interpreter.  The filter, defined in the Administrator tool, calls the ARF plug-in.  The ARF plug-in is managed by the arplugin service, a companion server working alongside the AR System server.  The ARF plug-in handles the interaction between AR System and the Perl interpreter.

    Note:  Click images for full screen view.

    Figure 1 - How the ARF Perl Plug-in and Perl Interpreter Fit into the Filter Processing.

    Examples

    The following examples cover some complex tasks that you might want to implement in a script.  In these examples, you may need to transform data from one format to another, perform a complex calculation, or contact another application for information.

    Complex String Manipulation

    The first example involves string manipulation, which is one of Perl's true strengths.  You can do an awful lot with very little code.  This Perl script will take a list of 10-digit phone numbers stored in an array called "numbers" and reformat each to look like the following:  "(555) 555-5555".

    for (my $x = 0; $x <= $#numbers; $x++)
    {
    # remove all non-numeric
    $numbers[$x] =~ s/[^0-9]+//g;
    # (xxx) xxx-xxxx
    $numbers[$x] =~ s/([0-9]{3})([0-9]{3})([0-9]{4})\Z/($1) $2-$3/;
    }

    Figure 2 shows the form provided with the ARF Perl plug-in so you can experiment with the examples.  The key things to note are the use of the &#39;Return Array Name&#39; and the input and output fields.  The script in the &#39;Perl Code&#39; field references the array name.

    Figure 2 - Example:  How to Use Embedded Perl Scripts in Your AR System Applications.

    Accessing Web Services

    The next examples show functionality that is also available in AR System&#39;s support of Web Services.  We include these examples because they show how to access other applications or services to acquire information.

    In this example, the Perl code takes an ISBN number provided in &#39;Input 1,&#39; invokes a Web Service, and returns the suggested price in &#39;Output 2&#39; when the entry is saved.

    use SOAP::Lite;
    my $s = SOAP::Lite
         -> uri(&#39;urn:xmethods-BNPriceCheck&#39;)
         -> proxy(&#39;http://services.xmethods.net/soap/servlet/rpcrouter',
               timeout => 20);
    $a[1] = $s->getPrice(SOAP::Data->type(string => $a[0]))->result;

    Here is an example of retrieving a NASDAQ quote.  Specify the ticker symbol in &#39;Input 1&#39; and the price will appear in &#39;Output 2&#39; when the entry is saved.

    use SOAP::Lite;
    my $s = SOAP::Lite  
         -> uri(&#39;urn:xmethods-delayed-quotes&#39;)
         -> proxy(&#39;http://services.xmethods.net/soap',
                  timeout => 20);
    $a[1] = $s->getQuote($a[0])->result;

    Both examples of Web Services use a &#39;Return Array Name&#39; of &quot;a.&quot;  Both are also accessible via AR System&#39;s built-in Web Service capabilities.

    When accessing an outside service, always specify a timeout as shown in the example above.

    Recent Enhancements

    When the ARF Perl plug-in was first introduced, it allowed you to specify a Perl script and the name of the array used to return data to AR System.  The downside of this version was that in order to pass field values into the script as input, you had to manufacture the script with filter set field actions on every invocation.

    The new version, arfperl 5.0 version 2, allows you to pass data into the script using the same array.  That is, the array is used for both input and output. Additionally, the new version is library-friendly and will properly load modules referenced in your Perl scripts (e.g. SOAP::Lite in the example above).

    Summary

    Imagine the possibilities... With an ARF plug-in, even very complex tasks can be tackled with very little effort using embedded scripting engines.  Download the ARF plug-in and examples from the AR System Developer Community download page.

    ~Rich
    Product Architect, Remedy Product Development
    &quot;When you come to a fork in the road, take it.&quot; Yogi Berra


    Figure 2 shows the form provided with the ARF Perl plug-in so you can experiment with the examples.  The key things to note are the use of the &#39;Return Array Name&#39; and the input and output fields.  The script in the &#39;Perl Code&#39; field references the array name.

    Figure 2 - Example:  How to Use Embedded Perl Scripts in Your AR System Applications.

    Accessing Web Services

    The next examples show functionality that is also available in AR System&#39;s support of Web Services.  We include these examples because they show how to access other applications or services to acquire information.

    In this example, the Perl code takes an ISBN number provided in &#39;Input 1,&#39; invokes a Web Service, and returns the suggested price in &#39;Output 2&#39; when the entry is saved.

    use SOAP::Lite;
    my $s = SOAP::Lite
         -> uri(&#39;urn:xmethods-BNPriceCheck&#39;)
         -> proxy(&#39;http://services.xmethods.net/soap/servlet/rpcrouter',
               timeout => 20);
    $a[1] = $s->getPrice(SOAP::Data->type(string => $a[0]))->result;

    Here is an example of retrieving a NASDAQ quote.  Specify the ticker symbol in &#39;Input 1&#39; and the price will appear in &#39;Output 2&#39; when the entry is saved.

    use SOAP::Lite;
    my $s = SOAP::Lite  
         -> uri(&#39;urn:xmethods-delayed-quotes&#39;)
         -> proxy(&#39;http://services.xmethods.net/soap',
                  timeout => 20);
    $a[1] = $s->getQuote($a[0])->result;

    Both examples of Web Services use a &#39;Return Array Name&#39; of &quot;a.&quot;  Both are also accessible via AR System&#39;s built-in Web Service capabilities.

    When accessing an outside service, always specify a timeout as shown in the example above.

    Recent Enhancements

    When the ARF Perl plug-in was first introduced, it allowed you to specify a Perl script and the name of the array used to return data to AR System.  The downside of this version was that in order to pass field values into the script as input, you had to manufacture the script with filter set field actions on every invocation.

    The new version, arfperl 5.0 version 2, allows you to pass data into the script using the same array.  That is, the array is used for both input and output. Additionally, the new version is library-friendly and will properly load modules referenced in your Perl scripts (e.g. SOAP::Lite in the example above).

    Summary

    Imagine the possibilities... With an ARF plug-in, even very complex tasks can be tackled with very little effort using embedded scripting engines.  Download the ARF plug-in and examples from the AR System Developer Community download page.

    ~Rich
    Product Architect, Remedy Product Development
    &quot;When you come to a fork in the road, take it.&quot; Yogi Berra