Danh mục

Embedding Perl in HTML with Mason Chapter 12: Custom Mason Subclasses- P1

Số trang: 22      Loại file: pdf      Dung lượng: 40.09 KB      Lượt xem: 9      Lượt tải: 0    
Thư viện của tui

Phí tải xuống: 8,000 VND Tải xuống file đầy đủ (22 trang) 0
Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu embedding perl in html with mason chapter 12: custom mason subclasses- p1, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Embedding Perl in HTML with Mason Chapter 12: Custom Mason Subclasses- P1Chapter 12: Custom Mason Subclasses- P1Something that we have tried very hard to do beginning with the 1.10 releaseof Mason is to make it easier to customize Masons behavior. Jon Swartzwas already on this track even back with the release of 0.80, which saw thefirst appearance of the HTML::Mason::Resolver classes, but 1.10 triesto bring this to new levels.Starting with 1.10 it has become possible to subclass almost every core classthat comes with Mason. Some obvious candidates for subclassing includethe Lexer, Compiler, and Resolver. This chapter will demonstrate how youmight go about implementing subclasses of various Mason objects.Class::Container as a SuperclassA number of modules in Mason are subclasses of Class::Container.This is a class that was created to encapsulate some common behaviors forMason objects. Originally, it was called HTML::Mason::Container,but Ken Williams decided to package this class separately and release it toCPAN, as it solves some fundamental problems of a large object-orientedsystem. Any Mason object that takes parameters to its constructor mustinherit from this module. Of course, since all of the classes that you mightconsider subclassing inherit from Class::Container already, youshouldnt need to inherit from it directly. However, you may need to usesome of its methods. We will briefly cover a few of them here, but see theClass::Container documentation for more details.The modules in the Mason core distribution that are Class::Containersubclasses are HTML::Mason::ApacheHandler,HTML::Mason::CGIHandler, HTML::Mason::Interp,HTML::Mason::Compiler, HTML::Mason::Lexer,HTML::Mason::Resolver, and HTML::Mason::Request.The most important methods that Class::Container provides arevalid_params() and contained_objects(), both of which areclass methods.The first, valid_params(), is called in order to register the validparameters for a classs new() constructor. The second method,contained_objects(), is used to register the objects, if any, that agiven class contains.The contained_objects() method is not something you will have touse for all of your subclasses, since most of the time you wont be alteringthe structure of Masons framework, youll just be plugging your own classesinto it. This method is called with a hash that contains as its keys parameternames that the classs constructor accepts and as its values the default nameof the contained class.For example, HTML::Mason::Compiler contains the following code: __PACKAGE__->contained_objects( lexer =>HTML::Mason::Lexer );This says that the HTML::Mason::Compiler->new() method willaccept a lexer parameter and that, if no such parameter is given, then anobject of the HTML::Mason::Lexer class will be constructed.Class::Container also implements a bit of magic here, so that ifHTML::Mason::Compiler->new() is called with a lexer_classparameter, it will load the class, instantiate a new object of that class, anduse that for the lexer. In fact, its even smart enough to notice if parametersgiven to HTML::Mason::Compiler->new() are really intended forthis subclass, and it will make sure that they get passed along.The valid_params() method is a bit more complex. It also takes a listof key/value pairs as arguments. The keys are the names of parametersaccepted by the new() method, while the values are hash referencesdefining a validation specification for the parameter. This specification islargely the same as that used by the Params::Validate module, with afew additions (but no subtractions).One addition is that each parameter, excluding those that represent containedobjects, may also define a value for parse. This tells Mason how to parsethis parameter if it is defined as part of an Apache configuration file. If noparse parameter is provided, a sensible default will be guessed from thevalue of the Params::Validatetype argument.The upshot of this is that your subclasses can define their own constructorparameters and Mason will then check for these parameters in an Apacheconfiguration file.As an example, HTML::Mason::Compiler contains the following: __PACKAGE__->valid_params ( allow_globals => { parse => list, type => ARRAYREF,default => [ ], descr => An array of names of Perlvariables that are . allowed globally withincomponents }, default_escape_flags => { parse => string, type => SCALAR,default => , descr => Escape flags that willapply by default to . all Mason tag output }, lexer => { isa => HTML::Mason::Lexer, descr => A Lexer object that will scancomponent . text during compi ...

Tài liệu được xem nhiều: