|
| 1 | + <chapter id="pl-perl"> |
| 2 | + <title>Perl Procedural Language</title> |
| 3 | + |
| 4 | + <para> |
| 5 | + This chapter describes how to compile, install and |
| 6 | + use PL/Perl. |
| 7 | + </para> |
| 8 | + <sect1> |
| 9 | + <title>Overview</title> |
| 10 | + <para> |
| 11 | + PL/Perl allows you to write functions in the Perl scripting |
| 12 | + language which may be used in SQL queries as if they were |
| 13 | + built into <productname>Postgres</productname>. |
| 14 | + </para> |
| 15 | + <para> |
| 16 | + The PL/Perl intepreter is a full Perl interpreter. However, |
| 17 | + certain operations have been disabled in order to increase |
| 18 | + the security level of the system. |
| 19 | + </para> |
| 20 | + <para> |
| 21 | + In general, the operations that are restricted are those that |
| 22 | + interact with the environment. This includes filehandle operations, |
| 23 | + <literal>require</literal>, and <literal>use</literal> (for external |
| 24 | + modules). |
| 25 | + </para> |
| 26 | + <para> |
| 27 | + It should be noted that this security is not absolute. Indeed, several |
| 28 | + Denial-of-Service attacks are still possible - memory exhaustion and |
| 29 | + endless loops are two. |
| 30 | + </para> |
| 31 | + </sect1> |
| 32 | + |
| 33 | + <sect1> |
| 34 | + <title>Building and Installing</title> |
| 35 | + <para> |
| 36 | + Assuming that the <productname>Postgres</productname> |
| 37 | + source tree is rooted at $PGSRC, then the Pl/perl source |
| 38 | + code is located in $PGSRC/src/pl/plperl. |
| 39 | + </para> |
| 40 | + <para> |
| 41 | + To build, simply do the following: |
| 42 | + <programlisting> |
| 43 | +cd $PGSRC/src/pl/plperl |
| 44 | +perl Makefile.PL |
| 45 | +make |
| 46 | + </programlisting> |
| 47 | + </para> |
| 48 | + |
| 49 | + <para> |
| 50 | + This will create a shared library file. On a Linux system, it will be |
| 51 | + named plperl.so. The extension may differ on other systems. |
| 52 | + </para> |
| 53 | + <para> |
| 54 | + The shared library should then be copied into the lib subdirectory of the |
| 55 | + postgres installation. |
| 56 | + </para> |
| 57 | + <para> |
| 58 | + The createlang command is used to install the language into a database. |
| 59 | + If it is installed into template1, all future databases will have the |
| 60 | + language installed automatically. |
| 61 | + </para> |
| 62 | + </sect1> |
| 63 | + |
| 64 | + <sect1> |
| 65 | + <title>Using PL/Perl</title> |
| 66 | + <para> |
| 67 | + Assume you have the following table: |
| 68 | + <programlisting> |
| 69 | +CREATE TABLE EMPLOYEE ( |
| 70 | + name text, |
| 71 | + basesalary int4, |
| 72 | + bonus int4 ); |
| 73 | + </programlisting> |
| 74 | + |
| 75 | + In order to get the total compensation (base + bonus) we could |
| 76 | + define a function as follows: |
| 77 | + <programlisting> |
| 78 | +CREATE FUNCTION totalcomp(int4, int4) RETURNS int4 |
| 79 | + AS 'return $_[0] + $_[1]' |
| 80 | + LANGUAGE 'plperl'; |
| 81 | + </programlisting> |
| 82 | + |
| 83 | + Note that the arguments are passed to the function in |
| 84 | + <literal>@_</literal> as might be expected. Also, because |
| 85 | + of the quoting rules for the SQL creating the function, you |
| 86 | + may find yourself using the extended quoting functions (qq[], |
| 87 | + q[], qw[]) more often that you are used to. |
| 88 | + </para> |
| 89 | + <para> |
| 90 | + We may now use our function like so: |
| 91 | + <programlisting> |
| 92 | +SELECT name, totalcomp(basesalary, bonus) from employee |
| 93 | + </programlisting> |
| 94 | + </para> |
| 95 | + <para> |
| 96 | + But, we can also pass entire tuples to our function: |
| 97 | + <programlisting> |
| 98 | +CREATE FUNCTION empcomp(employee) returns int4 |
| 99 | + AS 'my $emp = shift; |
| 100 | + return $emp->{'basesalary'} + $emp->{'bonus'};' |
| 101 | + LANGUAGE 'plperl'; |
| 102 | + </programlisting> |
| 103 | + A tuple is passed as a reference to hash. The keys are the names of |
| 104 | + fields in the tuples. The values are values of the corresponding |
| 105 | + field in the tuple. |
| 106 | + </para> |
| 107 | + <para> |
| 108 | + The new function <literal>empcomp</literal> can used like: |
| 109 | + <programlisting> |
| 110 | +SELECT name, empcomp(employee) from employee; |
| 111 | + </programlisting> |
| 112 | + </para> |
| 113 | + </sect1> |
| 114 | + </chapter> |
| 115 | + |
| 116 | +<!-- Keep this comment at the end of the file |
| 117 | +Local variables: |
| 118 | +mode:sgml |
| 119 | +sgml-omittag:nil |
| 120 | +sgml-shorttag:t |
| 121 | +sgml-minimize-attributes:nil |
| 122 | +sgml-always-quote-attributes:t |
| 123 | +sgml-indent-step:1 |
| 124 | +sgml-indent-data:t |
| 125 | +sgml-parent-document:nil |
| 126 | +sgml-default-dtd-file:"./reference.ced" |
| 127 | +sgml-exposed-tags:nil |
| 128 | +sgml-local-catalogs:("/usr/lib/sgml/CATALOG") |
| 129 | +sgml-local-ecat-files:nil |
| 130 | +End: |
| 131 | +--> |
0 commit comments