A Perl stored procedure or function behaves the same as a SQL stored procedure or function except that the code for the procedure or function is written in Perl and the execution of the procedure or function takes place outside the database server (that is, within a Perl executable instance).
It should be noted that there is a separate instance of the Perl executable for each connection that uses Perl stored procedures and functions. This behavior is different from Java stored procedures and functions. For Java, there is one instance of the Java VM for each database rather than one instance per connection. The other major difference between Perl and Java is that Perl stored procedures do not return result sets, whereas Java stored procedures can return result sets.
In addition to the above prerequisites, the database administrator must also install the Perl External Environment module.
perl Makefile.PL nmake nmake install
perl Makefile.PL make make install
Once the Perl external environment module has been built and installed, the Perl in the database support can be used.
START EXTERNAL ENVIRONMENT PERL;
ALTER EXTERNAL ENVIRONMENT PERL LOCATION 'perl-path';
ALTER EXTERNAL ENVIRONMENT PERL LOCATION 'c:\\Perl\\bin\\perl.exe';
Note that the START EXTERNAL ENVIRONMENT PERL statement is not necessary other than to verify that the database server can start Perl. In general, making a Perl stored procedure or function call starts Perl automatically.
Similarly, the STOP EXTERNAL ENVIRONMENT PERL statement is not necessary to stop an instance of Perl since the instance automatically goes away when the connection terminates. However, if you are completely done with Perl and you want to free up some resources, then the STOP EXTERNAL ENVIRONMENT PERL statement releases the Perl instance for your connection.
INSTALL EXTERNAL OBJECT 'perl-script' NEW FROM FILE 'perl-file' ENVIRONMENT PERL;
INSTALL EXTERNAL OBJECT 'perl-script' NEW FROM VALUE 'perl-statements' ENVIRONMENT PERL;
CREATE VARIABLE PerlVariable LONG VARCHAR; SET PerlVariable = 'perl-statements'; INSTALL EXTERNAL OBJECT 'perl-script' NEW FROM VALUE PerlVariable ENVIRONMENT PERL;
REMOVE EXTERNAL OBJECT 'perl-script'
INSTALL EXTERNAL OBJECT 'perl-script' UPDATE FROM FILE 'perl-file' ENVIRONMENT PERL INSTALL EXTERNAL OBJECT 'perl-script' UPDATE FROM VALUE 'perl-statements' ENVIRONMENT PERL SET PerlVariable = 'perl-statements'; INSTALL EXTERNAL OBJECT 'perl-script' UPDATE FROM VALUE PerlVariable ENVIRONMENT PERL
INSTALL EXTERNAL OBJECT 'SimplePerlExample' NEW FROM VALUE 'sub SimplePerlSub{ return( ($_[0] * 1000) + ($_[1] * 100) + ($_[2] * 10) + $_[3] ); }' ENVIRONMENT PERL; CREATE FUNCTION SimplePerlDemo( IN thousands INT, IN hundreds INT, IN tens INT, IN ones INT) RETURNS INT EXTERNAL NAME '<file=SimplePerlExample> $sa_perl_return = SimplePerlSub( $sa_perl_arg0, $sa_perl_arg1, $sa_perl_arg2, $sa_perl_arg3)' LANGUAGE PERL; // The number 1234 should appear SELECT SimplePerlDemo(1,2,3,4);
INSTALL EXTERNAL OBJECT 'PerlConsoleExample' NEW FROM VALUE 'sub WriteToServerConsole { print $sa_output_handle $_[0]; }' ENVIRONMENT PERL; CREATE PROCEDURE PerlWriteToConsole( IN str LONG VARCHAR) EXTERNAL NAME '<file=PerlConsoleExample> WriteToServerConsole( $sa_perl_arg0 )' LANGUAGE PERL; // 'Hello world' should appear in the database server messages window CALL PerlWriteToConsole( 'Hello world' );
CREATE TABLE perlTab(c1 int, c2 char(128)); INSTALL EXTERNAL OBJECT 'ServerSidePerlExample' NEW FROM VALUE 'sub ServerSidePerlSub { $sa_perl_default_connection->do( "INSERT INTO perlTab SELECT table_id, table_name FROM SYS.SYSTAB" ); $sa_perl_default_connection->do( "COMMIT" ); }' ENVIRONMENT PERL; CREATE PROCEDURE PerlPopulateTable() EXTERNAL NAME '<file=ServerSidePerlExample> ServerSidePerlSub()' LANGUAGE PERL; CALL PerlPopulateTable(); // The following should return 2 identical rows SELECT count(*) FROM perlTab UNION ALL SELECT count(*) FROM SYS.SYSTAB;