Perl Execution Context Overview

 

Introduction

The Bellhop Perl Execution Context plug-in executes services that are implemented in the Perl scripting language. The plug-in makes use of the default Perl installation bundled with Mac OS X (/usr/bin/perl), and executes each service in a spawned NSTask that invokes the Perl interpreter with the service's code.

To support Perl services, the plug-in extends Perl by loading a custom extension module into the Perl interpreter, making available three packages to your Perl services. These packages, Pasteboard, Panel and Finder, allow Perl scripts to interact with the pasteboard, interact with users, as well as invoke file functions via the Finder. The extensions are explained in more detail in the Reference part of this document.

 

Basic Syntax

Perl scripts can be structured quite freely; the only requirement is that the script contain a run subroutine defined at the top level of your Perl script. The name of this subroutine should be runService() and it should expect a single string argument that specifies the name of the pasteboard instance responsible for a service transaction. The listing below shows the basic structure of a Perl service script.

sub runService { my $pbrd = shift(@_); # Named pasteboard for this service transaction # Put your custom Perl code below. # . . . }

The $pbrd argument will be a string that specifies the unique pasteboard name for the service transaction, and is automatically passed to the function by Bellhop.

You are not required to explicitly load the Bellhop module into your service; it is automatically loaded when a Perl service is executed. You can, however, import any installed Perl modules and libraries you have on your system and use them within your service using any valid Perl syntax. You can also define your own classes, additional methods, constants, etc., so long as you provide a valid runService() subroutine at the top level.

For a more detailed Perl example, see the Example section of this document.

 

Perl Bellhop Packages

As metioned above, the Bellhop Perl plug-in implement a custom extension module containing three packages that allow Perl services to interact with the Mac OS X Services System, as well as interact with users through a set of custom user interface panels and do basic file manipulation via the Finder.

There are three Perl packages that you can make use of in your services: Pasteboard, Panel and Finder. The Pasteboard package provides methods that allow you to interact with the system pasteboard, which is used to pass data between the requesting application and your service. You can also use the Pasteboard package to write data to the standard clipboard as well. The Panel package provides methods that display user interface panels to users. There are panels to display alerts, prompt panels that allow your services to accept user input, and file selection and save panels. The Finder module has methods that allow you to do basic file manipulation, such as copy, move and delete files, as well as ask the Finder to open files, urls and applications.

For a complete description of the package functions, see the Reference section of this document.

Special care must be taken when writing Perl services that involve arrays and hashes. Unlike strings and numbers, which are passed around as normal scalar values, arrays and hashes are always passed into functions and returned from functions as references, so as to allow for nested structures. You must be aware of this when writing your own services.