Danh mục

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

Số trang: 27      Loại file: pdf      Dung lượng: 55.08 KB      Lượt xem: 10      Lượt tải: 0    
Thu Hiền

Phí tải xuống: 17,000 VND Tải xuống file đầy đủ (27 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- p2, 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- P2Chapter 12: Custom Mason Subclasses- P2Output: Compiling to a Different OutputSo youve decided that you really hate Mason and you want to use Embperlinstead. But you have a number of Mason components youve alreadywritten that youd like to save. Well, you can create your own compiler togenerate Embperl code from Mason. In this case, well use the lexer as is andrewrite the compiler from scratch. There isnt really a one-to-one matchbetween Mason and Embperls features so this example will, like the lexerexample, be limited in scope. Finding an intelligent way to convert Masonsmethods and subcomponents to Embperl is beyond the scope of this book.In case you are unfamiliar with Embperl, it uses the following syntax: [++] tags contain code whose results should be sent to the browser, likeMasons substitution tag (). The [* *] tags contain Perl code that isnot intended to generate output. This is equivalent to Masons % -lines and blocks. Finally, Embperl also has a [! !] tag similar to Masons block.There are other Embperl tags but, once again, this is a simplified example.Embperl does have a feature similar to Masons inheritance system calledEmbperlObject, but translating between the two is nontrivial.So lets make our new compiler: package HTML::Mason::Compiler::ToEmbperl; $VERSION = 0.01; use strict; use HTML::Mason::Lexer; use HTML::Mason::Exceptions ( abbr =>[qw(syntax_error)] ); use HTML::Mason::Compiler; use base qw(HTML::Mason::Compiler);This pulls in the basic packages well need. Even though we really arentinheriting much from HTML::Mason::Compiler , we still subclass it asanything expecting a compiler will check that what it is given is a subclassof HTML::Mason::Compiler.Of course, in our case, we wont be using this compiler with theHTML::Mason::Interp class, so the point is moot but important tomention. sub compile { my ($self, %p) = @_; $self->lexer->lex( comp_source =>$p{comp_source}, name => Embperl, compiler => $self ); return $self->component_as_embperl; }The only parameter we expect iscomp_source. We tell the lexer the name of the component isEmbperl since we dont really care what the name is in this context.Presumably we are being called by some sort of script that is simply going totake the Embperl-ized component and write it to disk somewhere. The nameis used for reporting syntax errors when a component is run, but that wontbe an issue in this case. sub start_component { my $self = shift; $self->{once_header} = ; $self->{header} = ; $self->{body} = ; $self->{footer} = ; $self->{current_block} = ; }This method is called to give the compiler a chance to reset its state, so thatswhat we do.We will be storing blocks of code in each of the first four attributes. Whenwe encounter a block, it will go in the once_header attribute.For blocks, we can put then in the header attribute. % -lines, blocks, blocks, substitution tags, and text will beplaced immediately into the body attribute. Finally, any blocks will go into the footer attribute.The current_block() attribute will be used to keep track of what typeof block we are in after a call to our start_block() method.This example will ignore other Mason syntax such as component calls,subcomponents, methods, and . Again, this will be left as anexercise for the reader. sub start_block { my ($self, %p) = @_; syntax_error Cannot nest a $p{block_type}inside a $self->{in_block} block if $self->{in_block};This is to make sure that the component is following the syntax rules weexpect. $self->{in_block} = $p{block_type}; }Then we record what kind of block we are starting, which will be somethinglike init or perl .The next method, raw_block() , is called for all of the blocks that wehandle except the block: sub raw_block { my ($self, %p) = @_; for ($self->{in_block}) { /^once$/ and $self->{once_header}.= $p{block}; /^init$/ and $self->{header}.= $p{block}; /^perl$/ and $self->{body}.= [* $p{block} *]; /^cleanup$/ and $self->{footer}.= $p{block}; } }This switchlike statement stores the code given to us in the appropriateattribute. If it is a block, we wrap it in the relevant Embperl tag;otherwise, we simply store it as is in the appropriate slot. sub text_block { my ($self, %p) = @_; $self->{body} .= $p{ ...

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