Danh mục

Embedding Perl in HTML with Mason Chapter 5: Advanced Features-P1

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

Phí tải xuống: 20,000 VND Tải xuống file đầy đủ (23 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 5: advanced features-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 5: Advanced Features-P1 Chapter 5: Advanced Features-P1In the previous chapters you have been introduced to the basic features ofMason, and you should have a fairly good idea by now of how you mightactually go about constructing a dynamic web site from Mason components.You have seen a few of Masons unique features, such as the autohandlermechanism, the dhandler mechanism, and the ability to pass arbitrary databetween components.In this chapter well go beyond the basics and learn more about advancedways to use Mason components to design large dynamic sites. Youll learnhow to define multiple components in the same text file, how to createcomponents on the fly from Perl strings, how to manage multiple componentroot directories, and (finally!) how to use all of Masons object-orientedfeatures.SubcomponentsAlthough we often imagine a one-to-one correspondence between text filesand Mason components, it is actually possible to define multiple componentsin a single text file. This is achieved by using a block, aspecial Mason directive that defines one component from within another.The component embedded within the block is called asubcomponent , and it is visible only to the component within which itresides: component A may not access component Bs subcomponentsdirectly.The subcomponent may use any of the standard Mason componentdirectives, such as , , %-lines, and so on. The onlyexceptions are that you may not use or blocks withinsubcomponents nor may you use global blocks like or.Subcomponents are most useful when you have some piece of processing torepeat several times that is used only in a certain specific situation anddoesnt merit its own separate component file.Here is an example of defining and calling a subcomponent. Note that thecomponent is assigned a name inside the tag (the name often startswith a period, purely by convention) and that you use the regularcomponent-calling mechanisms ($m->comp() or a tag) to invokeit. Information about certain Minnesotacities: % my @cities = (Young America, Sleepy Eye,Nisswa, Embarrass, % Saint Cloud, Little Canada,Burnsville, Luverne); % foreach my $name (@cities) { $name, state => MN &> % } $city $state Population: Coordinates: Mayor: my ($population, $latitude, $longitude, $mayor)= $dbh->selectrow_array(SELECT population,latitude, longitude, mayor FROM cities WHERE city=? andstate=?, undef, $city, $state); Since a subcomponent is visible only to the component that defines, andbecause it has all the capabilities that regular components have, you maythink of subcomponents as roughly analogous to privately scopedanonymous subroutine references in Perl.Creating Components on the FlyYou may encounter situations in which you want to use Masons templatingfeatures and data management tools, but you dont want to create a full-blown component root hierarchy on disk to house your components. Perhapsyou want to create a component from an isolated file or directly from astring containing the component text.For these situations, the Mason interpreter provides themake_component() method. It accepts a comp_file orcomp_source parameter (letting you create a component from a file or astring, respectively) and returns a Component object. # Creating a component from scratch #!/usr/bin/perl -w use strict; use HTML::Mason; my $source = $planet Hello, ! EOF my $interp = HTML::Mason::Interp->new( ); my $comp = $interp->make_component(comp_source =>$source); $interp->exec($comp, planet => Neptune);And here is a component that creates another component at runtime: my $comp = $m->interp->make_component( comp_file => /home/slappy/my_comps/foo, ); Of course, creating components at runtime is slower than creating themahead of time, so if you need to squeeze out all the performance youpossibly can, you might need to think of a speedier method to achieve yourgoals. And as always, benchmark everything so you really know what theeffects are.If the compiler encounters syntax errors when attempting to compile thecomponent, a fatal exception will be thrown inside themake_component() method. If you want to trap these errors, you maywrap the make_component() method in Perls eval {} block, andcheck $@ after the method call.Sharing Data Among Component SectionsBy default, the scope of variables created within an block, a Perlline, or any othe ...

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