![]() If the function result is type bool, it will be true or false according to whether Perl would evaluate the returned value as true. When this transform is applied, bool arguments will be seen by Perl as being 1 or empty, thus properly true or false. Then use the TRANSFORM function attribute for a PL/Perl function that takes or returns bool, for example:ĬREATE FUNCTION perl_and(bool, bool) RETURNS bool To use it, install the extension:ĬREATE EXTENSION bool_plperl - or bool_plperlu for PL/PerlU Suitable transforms are provided by the bool_plperl extension. This is problematic, since Perl will not treat 'f' as false! It is possible to improve matters by using a “ transform” (see CREATE TRANSFORM). As just stated, the default behavior for bool values is that they are passed to Perl as text, thus either 't' or 'f'. ![]() One case that is particularly important is boolean values. For example, the encode_bytea function can be used to escape binary data for a return value of type bytea. Similarly, values passed back to PostgreSQL must be in the external text representation format. For example, the decode_bytea function can be used to convert an argument of type bytea into unescaped binary. However, in other cases the argument will need to be converted into a form that is more usable in Perl. In the case of ordinary numeric or text types, Perl will just do the right thing and the programmer will normally not have to worry about it. This can be done whether the function is strict or not.Īnything in a function argument that is not a reference is a string, which is in the standard PostgreSQL external text representation for the relevant data type. For example, suppose that we wanted perl_max with one null and one nonnull argument to return the nonnull argument, rather than a null value:Īs shown above, to return an SQL null value from a PL/Perl function, return an undefined value. Alternatively, we could check for undefined inputs in the function body. We could add STRICT to the function definition to make PostgreSQL do something more reasonable: if a null value is passed, the function will not be called at all, but will just return a null result automatically. ![]() The above function definition will not behave very nicely with null inputs (in fact, it will act as though they are zeroes). ![]() If an SQL null value is passed to a function, the argument value will appear as “ undefined” in Perl. If you choose to use escape string syntax E'', you must double any single quote marks ( ') and backslashes ( \) used in the body of the function (see Section 4.1.2.1).Īrguments and results are handled as in any other Perl subroutine: arguments are passed in and a result value is returned with return or as the last expression evaluated in the function.įor example, a function returning the greater of two integer values could be defined as:ĬREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$Īrguments will be converted from the database's encoding to UTF-8 for use inside PL/Perl, and then converted from UTF-8 back to the database encoding upon return. It is usually most convenient to use dollar quoting (see Section 4.1.2.4) for the string constant. The syntax of the CREATE FUNCTION command requires the function body to be written as a string constant. For more information, see the entries for Variable "%s" will not stay shared and Variable "%s" is not available in the perldiag man page, or search the Internet for “ perl nested named subroutine”. In general, it is far safer to create anonymous subroutines which you call via a coderef. Because a PL/Perl function is wrapped in a subroutine, any named subroutine you place inside one will be nested. The use of named nested subroutines is dangerous in Perl, especially if they refer to lexical variables in the enclosing scope.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |