Book Image

PostgreSQL Server Programming - Second Edition

Book Image

PostgreSQL Server Programming - Second Edition

Overview of this book

Table of Contents (21 chapters)
PostgreSQL Server Programming Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Untrusted Perl


We discussed untrusted PL/PythonU in Chapter 8, Using Unrestricted Languages. PL/Perl is also available as an untrusted language. The trusted version runs inside a security context that does not allow interaction with the environment. Just like PL/Pythonu, we can bypass the security restrictions using PL/Perlu or the untrusted version. Let's rewrite the directory listing function list_folder from Chapter 8, Listing directory contents to a Perl equivalent:

CREATE OR REPLACE FUNCTION list_folder_plperl(directory VARCHAR) RETURNS SETOF TEXT
AS $$
  my $d = shift;
  opendir(D, "$d") || elog (ERROR,'Cant open directory '.$d) ;
  my @list = readdir(D);
  closedir(D);

  foreach my $f (@list) {
    return_next($f);
  }
  return undef;
$$ LANGUAGE plperlu;

Let's run our function, as shown here:

testdb=# SELECT list_folder_plperl('/usr/local/pgsql/bin');                                                                                                                                    ...