You are not logged in Log in Join
You are here: Home » Members » andym » Using a Script (Perl)

Log in
Name

Password

 

Using a Script (Perl)

Note: This is currently unfinished.

A Script (Perl) has been designed to look and feel very similar to a python script. Basically you can write Perl directly TTW and call it like any other object.

Important Note

  1. Python scripts stop you from doing many nasty things such as infinite loops, nasty regex's and such. Perl scripts do not, be careful who you let use them. This is also a big advantage, we finally get regex's easily accessible... hooray!
  2. You can't do the "return printed" trick Script (Python) allows.

Example 1: self

Arguments: self
Code:
my @res;

for ($self->objectValues()) {
 push(@res, $_->getId());
}
return join("\n", @res);

Click "edit" to save the code and "Try it" to run. If when clicking the "edit" button you get the message: "Global symbol "$self" requires explicit package name at test line 3.", this means you did not enter self as an argument in the box.

When you click "Try it" you will note that you get a message: "self: Provided by Zope's ZPublisher internally ". This is roughly the methods container (thats why this listing includes the method). Click execute to run, you should then get a listing of the objects in your directory.

Notes:

  1. use self to get the container.
  2. the perl equivalent of . is ->, allowing access to sub-objects / methods
  3. "use strict;" is enabled on all scripts

Example 2: subs, passing arguments

Arguments: self
Code:
my @res;
search($self);

sub search {
 my @array = ('Folder', 'DTML Method');

 for ($_[0]->objectValues(\@array)) {
  push(@res, $_->getId());
 }
}

return join("\n", @res);

You can happily define subs within a Script, such as "sub search". Here we also pass through a reference to an array containing the objects we want to list.

Urlify

Example 3: something useful

Arguments: data
Code:
my $urls = '(http|telnet|gopher|file|wais|ftp|mailto)';
my $ltrs = '\w';
my $gunk = '/#~:.?+=&%@!\-';
my $punc = '.:?\-';
my $any  = "${ltrs}${gunk}${punc}";

$data =~ s{
      \b                    # start at word boundary
      (                     # begin $1  {
       $urls     :          # need resource and a colon
       [$any] +?            # followed by on or more
                            #  of any valid character, but
                            #  be conservative and take only
                            #  what you need to....
      )                     # end   $1  }
      (?=                   # look-ahead non-consumptive assertion
       [$punc]*             # either 0 or more punctuation
       [^$any]              #   followed by a non-url char
       |                    # or else
       $                    #   then end of the string
      )
     }{$1}igox;

return $data;

When you run it, give data a value containing a URL eg: activestate is at http://www.activestate.com. The regex urlify's the code.

Passing arguments

There are several ways to do it that defer from a more traditional DTML style. Firstly an internal perl is running in strict mode so you must declare your variables with a my, except for ones you are passing, place the variables you are passing in the arguments section. For example in the above example it is looking for data as an argument this can be passed in the following ways:

http://www.foo.com/urlify?data=sampletext

<dtml-var "urlify('this is the text')">

<dtml-var "urlify(data)"> # where data is in the REQUEST from a POST or other action.