|
1 | 1 | <!-- |
2 | | -$Header: /cvsroot/pgsql/doc/src/sgml/plperl.sgml,v 2.5 2000/09/29 20:21:34 petere Exp $ |
| 2 | +$Header: /cvsroot/pgsql/doc/src/sgml/plperl.sgml,v 2.6 2000/12/19 18:16:25 petere Exp $ |
3 | 3 | --> |
4 | 4 |
|
5 | | - <chapter id="pl-perl"> |
6 | | - <title>PL/perl - Perl Procedural Language</title> |
| 5 | +<chapter id="plperl"> |
| 6 | + <title>PL/Perl - Perl Procedural Language</title> |
| 7 | + |
| 8 | + <para> |
| 9 | + PL/Perl allows you to write functions in the Perl programming |
| 10 | + language which may be used in SQL queries as if they were built into |
| 11 | + <productname>Postgres</productname>. |
| 12 | + </para> |
| 13 | + |
| 14 | + <para> |
| 15 | + The PL/Perl intepreter is a full Perl interpreter. However, certain |
| 16 | + operations have been disabled in order to maintain the security of |
| 17 | + the system. In general, the operations that are restricted are |
| 18 | + those that interact with the environment. This includes filehandle |
| 19 | + operations, <literal>require</literal>, and <literal>use</literal> |
| 20 | + (for external modules). It should be noted that this security is |
| 21 | + not absolute. Indeed, several Denial-of-Service attacks are still |
| 22 | + possible - memory exhaustion and endless loops are two examples. |
| 23 | + </para> |
| 24 | + |
| 25 | + <sect1 id="plperl-install"> |
| 26 | + <title>Building and Installing</title> |
7 | 27 |
|
8 | 28 | <para> |
9 | | - This chapter describes how to compile, install and |
10 | | - use PL/Perl. |
| 29 | + In order to build and install PL/Perl if you are installing |
| 30 | + <productname>Postgres</productname> from source then the |
| 31 | + <option>--with-perl</option> must be supplied to the |
| 32 | + <filename>configure</filename> script. PL/Perl requires that, when |
| 33 | + <productname>Perl</productname> was installed, the |
| 34 | + <filename>libperl</filename> library was build as a shared object. |
| 35 | + At the time of this writing, this is almost never the case in the |
| 36 | + Perl packages that are distributed with the operating systems. A |
| 37 | + message like this will appear during the build to point out this |
| 38 | + fact: |
| 39 | +<screen> |
| 40 | +<computeroutput> |
| 41 | +***** |
| 42 | +* Cannot build PL/Perl because libperl is not a shared library. |
| 43 | +* Skipped. |
| 44 | +***** |
| 45 | +</computeroutput> |
| 46 | +</screen> |
| 47 | + Therefore it is likely that you will have to re-build and install |
| 48 | + <productname>Perl</productname> manually to be able to build |
| 49 | + PL/Perl. |
11 | 50 | </para> |
12 | | - <sect1 id="plperl-overview"> |
13 | | - <title>Overview</title> |
14 | | - <para> |
15 | | - PL/Perl allows you to write functions in the Perl scripting |
16 | | - language which may be used in SQL queries as if they were |
17 | | - built into <productname>Postgres</productname>. |
18 | | - </para> |
19 | | - <para> |
20 | | - The PL/Perl intepreter is a full Perl interpreter. However, |
21 | | - certain operations have been disabled in order to increase |
22 | | - the security level of the system. |
23 | | - </para> |
24 | | - <para> |
25 | | - In general, the operations that are restricted are those that |
26 | | - interact with the environment. This includes filehandle operations, |
27 | | - <literal>require</literal>, and <literal>use</literal> (for external |
28 | | - modules). |
29 | | - </para> |
30 | | - <para> |
31 | | - It should be noted that this security is not absolute. Indeed, several |
32 | | - Denial-of-Service attacks are still possible - memory exhaustion and |
33 | | - endless loops are two. |
34 | | - </para> |
35 | | - </sect1> |
36 | 51 |
|
37 | | - <sect1 id="plperl-install"> |
38 | | - <title>Building and Installing</title> |
39 | | - <para> |
40 | | - Assuming that the <productname>Postgres</productname> |
41 | | - source tree is rooted at $PGSRC, then the Pl/perl source |
42 | | - code is located in $PGSRC/src/pl/plperl. |
43 | | - </para> |
44 | | - <para> |
45 | | - To build, simply do the following: |
46 | | - <programlisting> |
47 | | -cd $PGSRC/src/pl/plperl |
48 | | -perl Makefile.PL |
49 | | -make |
50 | | - </programlisting> |
51 | | - </para> |
| 52 | + <para> |
| 53 | + When you want to retry to build PL/Perl after having reinstalled |
| 54 | + Perl, then change to the directory |
| 55 | + <filename>src/pl/plperl</filename> in the |
| 56 | + <productname>Postgres</productname> source tree and issue the commands |
| 57 | +<programlisting> |
| 58 | +gmake clean |
| 59 | +gmake all |
| 60 | +gmake install |
| 61 | +</programlisting> |
| 62 | + </para> |
52 | 63 |
|
53 | | - <para> |
54 | | - This will create a shared library file. On a Linux system, it will be |
55 | | - named plperl.so. The extension may differ on other systems. |
56 | | - </para> |
57 | | - <para> |
58 | | - The shared library should then be copied into the lib subdirectory of the |
59 | | - postgres installation. |
60 | | - </para> |
61 | | - <para> |
62 | | - The createlang command is used to install the language into a database. |
63 | | - If it is installed into template1, all future databases will have the |
64 | | - language installed automatically. |
65 | | - </para> |
66 | | - </sect1> |
| 64 | + <para> |
| 65 | + The <command>createlang</command> command is used to install the |
| 66 | + language into a database. |
| 67 | +<screen> |
| 68 | +<prompt>$</prompt> <userinput>createlang plperl template1</userinput> |
| 69 | +</screen> |
| 70 | + If it is installed into template1, all future databases will have |
| 71 | + the language installed automatically. |
| 72 | + </para> |
| 73 | + </sect1> |
67 | 74 |
|
68 | | - <sect1 id="plperl-use"> |
69 | | - <title>Using PL/Perl</title> |
70 | | - <para> |
71 | | - Assume you have the following table: |
72 | | - <programlisting> |
| 75 | + <sect1 id="plperl-use"> |
| 76 | + <title>Using PL/Perl</title> |
| 77 | + |
| 78 | + <para> |
| 79 | + Assume you have the following table: |
| 80 | +<programlisting> |
73 | 81 | CREATE TABLE EMPLOYEE ( |
74 | 82 | name text, |
75 | | - basesalary int4, |
76 | | - bonus int4 ); |
77 | | - </programlisting> |
78 | | - |
79 | | - In order to get the total compensation (base + bonus) we could |
80 | | - define a function as follows: |
81 | | - <programlisting> |
82 | | -CREATE FUNCTION totalcomp(int4, int4) RETURNS int4 |
| 83 | + basesalary integer, |
| 84 | + bonus integer |
| 85 | +); |
| 86 | +</programlisting> |
| 87 | + |
| 88 | + In order to get the total compensation (base + bonus) we could |
| 89 | + define a function as follows: |
| 90 | +<programlisting> |
| 91 | +CREATE FUNCTION totalcomp(integer, integer) RETURNS integer |
83 | 92 | AS 'return $_[0] + $_[1]' |
84 | 93 | LANGUAGE 'plperl'; |
85 | | - </programlisting> |
| 94 | +</programlisting> |
86 | 95 |
|
87 | | - Note that the arguments are passed to the function in |
88 | | - <literal>@_</literal> as might be expected. Also, because |
89 | | - of the quoting rules for the SQL creating the function, you |
90 | | - may find yourself using the extended quoting functions (qq[], |
91 | | - q[], qw[]) more often that you are used to. |
92 | | - </para> |
93 | | - <para> |
94 | | - We may now use our function like so: |
95 | | - <programlisting> |
96 | | -SELECT name, totalcomp(basesalary, bonus) from employee |
97 | | - </programlisting> |
98 | | - </para> |
99 | | - <para> |
100 | | - But, we can also pass entire tuples to our function: |
101 | | - <programlisting> |
102 | | -CREATE FUNCTION empcomp(employee) returns int4 |
103 | | - AS 'my $emp = shift; |
104 | | - return $emp->{''basesalary''} + $emp->{''bonus''};' |
105 | | - LANGUAGE 'plperl'; |
106 | | - </programlisting> |
107 | | - A tuple is passed as a reference to hash. The keys are the names of |
108 | | - fields in the tuples. The values are values of the corresponding |
109 | | - field in the tuple. |
110 | | - </para> |
| 96 | + Notice that the arguments to the function are passed in |
| 97 | + <varname>@_</varname> as might be expected. |
| 98 | + </para> |
| 99 | + |
| 100 | + <para> |
| 101 | + We can now use our function like so: |
| 102 | +<programlisting> |
| 103 | +SELECT name, totalcomp(basesalary, bonus) FROM employee; |
| 104 | +</programlisting> |
| 105 | + </para> |
| 106 | + |
| 107 | + <para> |
| 108 | + But, we can also pass entire tuples to our functions: |
| 109 | +<programlisting> |
| 110 | +CREATE FUNCTION empcomp(employee) RETURNS integer AS ' |
| 111 | + my $emp = shift; |
| 112 | + return $emp->{''basesalary''} + $emp->{''bonus''}; |
| 113 | +' LANGUAGE 'plperl'; |
| 114 | +</programlisting> |
| 115 | + A tuple is passed as a reference to a hash. The keys are the names |
| 116 | + of the fields in the tuples. The hash values are values of the |
| 117 | + corresponding fields in the tuple. |
| 118 | + </para> |
| 119 | + |
| 120 | + <tip> |
111 | 121 | <para> |
112 | | - The new function <literal>empcomp</literal> can used like: |
113 | | - <programlisting> |
114 | | -SELECT name, empcomp(employee) from employee; |
115 | | - </programlisting> |
| 122 | + Because the function body is passed as an SQL string literal to |
| 123 | + <command>CREATE FUNCTION</command> you have to escape single |
| 124 | + quotes within your Perl source, either by doubling them as shown |
| 125 | + above, or by using the extended quoting functions |
| 126 | + (<literal>q[]</literal>, <literal>qq[]</literal>, |
| 127 | + <literal>qw[]</literal>). Backslashes must be escaped by doubling |
| 128 | + them. |
116 | 129 | </para> |
117 | | - </sect1> |
118 | | - </chapter> |
| 130 | + </tip> |
| 131 | + |
| 132 | + <para> |
| 133 | + The new function <function>empcomp</function> can used like: |
| 134 | +<programlisting> |
| 135 | +SELECT name, empcomp(employee) FROM employee; |
| 136 | +</programlisting> |
| 137 | + </para> |
| 138 | + |
| 139 | + <para> |
| 140 | + Here is an example of a function which will not work because file |
| 141 | + system operations are not allowed for security reasons: |
| 142 | +<programlisting> |
| 143 | +CREATE FUNCTION badfunc() RETURNS integer AS ' |
| 144 | + open(TEMP, ">/tmp/badfile"); |
| 145 | + print TEMP "Gotcha!\n"; |
| 146 | + return 1; |
| 147 | +' LANGUAGE 'plperl'; |
| 148 | +</programlisting> |
| 149 | + The creation of the function will succeed, but executing it will not. |
| 150 | + </para> |
| 151 | + |
| 152 | + </sect1> |
| 153 | +</chapter> |
119 | 154 |
|
120 | 155 | <!-- Keep this comment at the end of the file |
121 | 156 | Local variables: |
|
0 commit comments