Скачать презентацию Copyright 2001 Active State Using perl with Скачать презентацию Copyright 2001 Active State Using perl with

1be781108020d8ebdb46adfd6bf5ece8.ppt

  • Количество слайдов: 39

Copyright 2001, Active. State Copyright 2001, Active. State

Using perl with Zope Andy Mc. Kay, Active. State Python 9 Conference Copyright 2001, Using perl with Zope Andy Mc. Kay, Active. State Python 9 Conference Copyright 2001, Active. State

Agenda • About using Perl with Zope • Why Perl with Zope? • Example Agenda • About using Perl with Zope • Why Perl with Zope? • Example of use – Perl Restricted methods – Perl Unrestricted methods – Perl DBI • Perl from Python • Internals Copyright 2001, Active. State

Perl with Zope • Created by Active. State for Digital Creations. • Developed done Perl with Zope • Created by Active. State for Digital Creations. • Developed done by Gisle Aas. • Goal: "To provide an alternative to Python for scripting Zope in the same process. " • Currently in beta? Copyright 2001, Active. State

Perl with Zope does not… • Mean a rewriting of Zope • Allow the Perl with Zope does not… • Mean a rewriting of Zope • Allow the creation of a Perl only product • Zope speaks lots of different things (FTP, SOAP, Web. DAV etc. )… and now Perl Copyright 2001, Active. State

Perl with Zope does allow… • Creation of Perl Scripts through the web – Perl with Zope does allow… • Creation of Perl Scripts through the web – Perl Script – Similar to Python Script • Creation of Perl External Methods – Perl Script (Unrestricted) – Similar to External Method • Using Perl within Python Copyright 2001, Active. State

Why use perl with Zope? • Code reuse – Access to legacy scripts and Why use perl with Zope? • Code reuse – Access to legacy scripts and modules – Most things have already been done in perl – Perl Cookbook, Programming Perl etc… • Large number of Perl developers – Community and knowledge – Perl is on Linux and most people use Linux What OS do you primarily run Zope on? –Linux 43. 09% –Windows NT/2000 25. 97% –Windows 9 x 6. 91% –Unix 6. 91% 362 users polled, from www. Zope. Zen. org. Copyright 2001, Active. State

Why use perl with Zope? • CPAN – CPAN: Comprehensive Perl Archive Network – Why use perl with Zope? • CPAN – CPAN: Comprehensive Perl Archive Network – PPM: Perl Package Manager Copyright 2001, Active. State

Why use perl with Zope? • CPAN – CPAN: Comprehensive Perl Archive Network – Why use perl with Zope? • CPAN – CPAN: Comprehensive Perl Archive Network – PPM: Perl Package Manager – CPAN: 1, 964 modules – Zope. org: 296 products Copyright 2001, Active. State

Technical Overview • Two parts: – Pyperl • Python Perl integration, not Zope specific. Technical Overview • Two parts: – Pyperl • Python Perl integration, not Zope specific. • Current version 1. 0 – Zoperl • Depends on pyperl. Zope specific, platform independent code. –Script Perl (restricted) –Script Perl (unrestricted) –Z DBI Database Connection • Current version beta 0. 5 Copyright 2001, Active. State

Script Perl (restricted) • Zope object: Script (Perl) – Edited through the web – Script Perl (restricted) • Zope object: Script (Perl) – Edited through the web – Access to most of the Perl core – Can’t use modules – Runs in strict • Similar in idea to Script (Python) Copyright 2001, Active. State

Example, Object listing (DTML) • Get a listing of Zope objects using object. Ids() Example, Object listing (DTML) • Get a listing of Zope objects using object. Ids() in a container • In DTML… Example: List Copyright 2001, Active. State

Example, Object listing (Python) • In Script Python… – context is bound by default Example, Object listing (Python) • In Script Python… – context is bound by default in Python methods – can use print / printed shortcut for item in context. object. Ids(): print item return printed Example: List. PY Copyright 2001, Active. State

Example, Object listing (Perl) • In Script Perl… – self is declared as an Example, Object listing (Perl) • In Script Perl… – self is declared as an argument my @res; for ($self->object. Ids()) { push(@res, $_); } return join("n", @res); Example: List. PL Copyright 2001, Active. State

Gotchas • Can’t get an item (such as a folder) the same as python Gotchas • Can’t get an item (such as a folder) the same as python – context. folder. object. Ids() works. – $self->folder->object. Ids() doesn’t. Zope Error Type: Attribute. Error Value: __call__ Note: Perl errors are raised as Python errors… Copyright 2001, Active. State

Python: : getitem • Use Python: : getitem(. . ) – Arguments: object, subobject Python: : getitem • Use Python: : getitem(. . ) – Arguments: object, subobject id my @res; my $folder = Python: : getitem($self, ‘Control_Panel'); for ( (Python: : getitem($folder, 'Products'))->object. Ids() ) { if ($_ =~ m/perl/i) { push(@res, $_); } } return join("n", @res) Example: List. Products Copyright 2001, Active. State

The good, the bad and the ugly • Good – Regex’s, transliterations etc are The good, the bad and the ugly • Good – Regex’s, transliterations etc are available • Bad – Regex’s, transliterations etc are available – No loop limit – No bindings and other Python Script features – getitem • Ugly – Through the web editing – “Guilty as charged. Perl is happily ugly, and happily derivative”, Larry Wall Copyright 2001, Active. State

Script Perl, unrestricted • Zope object: Script (Perl, unrestricted) – Edited on file system Script Perl, unrestricted • Zope object: Script (Perl, unrestricted) – Edited on file system – Access to all of the Perl core – Can use modules – Can choose “strictness” • Similar in idea to External Method Copyright 2001, Active. State

Example (Calendar) • A web based calendar that uses some interesting CPAN modules… • Example (Calendar) • A web based calendar that uses some interesting CPAN modules… • Elements: – CPAN Modules – Actual perl module for finding monthly and daily information – Scripts (Perl, unrestricted) to call from DTML – DTML Method to display calendar Copyright 2001, Active. State

Calendar: Perl Setup • Installing CPAN modules: – Installed from CPAN or via PPM Calendar: Perl Setup • Installing CPAN modules: – Installed from CPAN or via PPM • File location: /Extensions/Zope. Ext/Calendar. pm #! /usr/bin/perl -w use strict; package Zope. Ext: : Calendar; # import modules use Date: : Calc; use Date: : Manip; … Example: Calendar. pm Copyright 2001, Active. State

Calendar: Perl Scripts sub Month { my ($year, $month, $day) = split('/', $_[0]); if Calendar: Perl Scripts sub Month { my ($year, $month, $day) = split('/', $_[0]); if (!Date: : Calc: : check_date ($year, $month, $day)) { Python: : raise('Date. Error', “Date not valid"); } } my %hash; $hash{'month_name'} = Date: : Calc: : Month_to_Text($month); … return %hash; } Note: Having "month" as an argument ensures I will be passed something. Example: Calendar. pm Copyright 2001, Active. State

Calendar: Perl Scripts (Alt) sub Day { my ($year, $month, $day); my $self = Calendar: Perl Scripts (Alt) sub Day { my ($year, $month, $day); my $self = shift; my %request = %{$self->{'REQUEST'} }; if (exists $request{'day'}) { ($year, $month, $day) = split('/', $request{'day'}); if (!Date: : Calc: : check_date ($year, $month, $day)) { Python: : raise('Date. Error', 'Date not valid'); } } else { Python: : raise('Date. Error', 'No day given'); } Note: Having "self" as an argument ensures I will be passed "self", so I can call REQUEST etc… Example: Calendar. pm Copyright 2001, Active. State

Calendar: Access • Requires hack to Access. Control to allow hashes and arrays to Calendar: Access • Requires hack to Access. Control to allow hashes and arrays to be returned without security machinery complaining. – from Chris Mc. Donough import Record Calendar: Perl Scripts try: import perlhash = perl. get_ref("%") except: perlhash = {} Container. Assertions ={ … type(perlhash): 1, … } Copyright 2001, Active. State

Calendar: Scripts • Create objects to be called. • Eg: – – Id: Calendar. Calendar: Scripts • Create objects to be called. • Eg: – – Id: Calendar. Month. PM Function name: Month Function arguments: month Perl module: Zope. Ext: : Calendar Example: Calendar. Month. PM Copyright 2001, Active. State

Calendar: DTML • DTML to show calendar • Returning a hash reference, uses mapping. Calendar: DTML • DTML to show calendar • Returning a hash reference, uses mapping. • For array use Example: Calendar. Example Copyright 2001, Active. State

Calendar: End Result Example: Calendar. Example Copyright 2001, Active. State Calendar: End Result Example: Calendar. Example Copyright 2001, Active. State

Gotchas • Use getattr and setattr to modify things such as REQUEST/ • Returning Gotchas • Use getattr and setattr to modify things such as REQUEST/ • Returning complex data structures can be tricky to DTML. Copyright 2001, Active. State

ZDBI DA • One other product, ZDBI_DA • Perl DBI has access to most ZDBI DA • One other product, ZDBI_DA • Perl DBI has access to most databases, can go where Python can’t… • A Python object that forwards requests to Perl DBI. • Roughly 30% performance hit over native drivers Copyright 2001, Active. State

Using Perl in Python • As of pyperl-1. 0, released Thursday. . . • Using Perl in Python • As of pyperl-1. 0, released Thursday. . . • Added in a wrapping around Perl objects so they can be called from Python. – Currently works best on Perl objects • Syntax: from perlmod import Perl • think of it as a dot (. ) instead of two colons in an object… Copyright 2001, Active. State

Using Perl in Python 1. 5. 2 (#0, Jul 30 1999, 09: 52: 18) Using Perl in Python 1. 5. 2 (#0, Jul 30 1999, 09: 52: 18) [MSC 32 bit (Intel)] on win 32 Copyright 1991 -1995 Stichting Mathematisch Centrum, Amsterdam >>> from perlmod import Perl >>> import time >>> s = Perl. Date. Tolkien. Shire(time()) >>> print s. on_date() Highday Rethe 11 7465 Gollum visits Shelob, 1419. … Copyright 2001, Active. State

Using Perl in a Zope Product • FSCounter, simple Zope product to provide a Using Perl in a Zope Product • FSCounter, simple Zope product to provide a hit counter. – File system used to avoid Data. fs bloat • Problem: – No simple and easy cross platform locking mechanism in Python – fcntl is Unix only • Solution: – File: : Counter from CPAN Copyright 2001, Active. State

Example: FSCounter from perlmod import Perl # perl … def write(self): ''' increment file, Example: FSCounter from perlmod import Perl # perl … def write(self): ''' increment file, perl ''' p = Perl. File. Counter. File(self. _filename()) return p. inc() def read(self): ''' read file, perl ''' p = Perl. File. Counter. File(self. _filename()) return p. value() Example: counter, footer Copyright 2001, Active. State

Internals • Full API not of interest here, whole talk all of its own. Internals • Full API not of interest here, whole talk all of its own. • Discussed by Gisle Aas in the paper. • Of interest: – Exceptions – Complex data types – Threading Copyright 2001, Active. State

Exceptions • Can raise an error by: – Python: : raise($type, $value) • The Exceptions • Can raise an error by: – Python: : raise($type, $value) • The Python: : Err: : -functions also return the standard python exception type objects if called without arguments. – Python: : raise(Python: : Err: : Index. Error, "out of range") Copyright 2001, Active. State

Complex data types • Py. Perl does conversion of simple data type (strings, int Complex data types • Py. Perl does conversion of simple data type (strings, int etc) • Perl ref object is a reference to a python object that encapsulates a perl reference. Python 1. 5. 2 (#0, Jul 30 1999, 09: 52: 18) [MSC 32 bit (Intel)] on win 32 Copyright 1991 -1995 Stichting Mathematisch Centrum, Amsterdam >>> import perl >>> inc = perl. get_ref("@INC") >>> inc >>> len(inc) 3 Copyright 2001, Active. State

Threading • Perl is multi-threaded when used with Python. • A Perl interpreter is Threading • Perl is multi-threaded when used with Python. • A Perl interpreter is opened up for each thread. • It is released when Zope releases its thread. • Perl references cannot be passed between Perl threads. Don’t try and pickle a Perl reference. Copyright 2001, Active. State

Future • Py. Perl the python-perl part of Zope Perl as part of PPM, Future • Py. Perl the python-perl part of Zope Perl as part of PPM, meaning very easy install. • Py. Perl finalised. • Zo. Perl the zope-perl part of Zope Perl as part of the Zope core? • Currently in beta, be finished RSN. Copyright 2001, Active. State

More information • Project homepage: www. zope. org/Wikis/zope-perl/Front. Page • Zope Perl available from: More information • Project homepage: www. zope. org/Wikis/zope-perl/Front. Page • Zope Perl available from: www. Active. State. com/download/Zope-Perl/ • Examples, plus this talk at: www. zope. org/Members/andym • Mailing list: Zope-Perl@Zope. org Copyright 2001, Active. State

Copyright 2001, Active. State Copyright 2001, Active. State