Danh mục

Embedding Perl in HTML with Mason Chapter 2: Components- P2

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

Phí tải xuống: 18,000 VND Tải xuống file đầy đủ (21 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 2: components- 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 2: Components- P2 Chapter 2: Components- P2This example demonstrates all the syntax possibilities for this block. First ofall, we have argument types and names. The valid types are scalar, array,and hash, represented by their corresponding Perl sigil ($, @, or %), exactlyas would be expected.It is possible to give an argument a default value to be used if none isprovided when the component is called. Any argument without a default isconsidered a required argument. Calling a component without specifying allits required arguments will cause a fatal exception to be thrown.An arguments default can refer to an earlier argument, so this is completelylegal: $x $y => $x * 2 > 20 ? 50 : 100 While this block looks as if it contains Perl, it is important to realize that itssyntax is actually something unique to Mason. Importantly, lines should notend with a semicolon or comma, and each variable definition must be on asingle line.It is possible to have comments both after an argument declaration and ontheir own line. Comments start with the # character and continue to the endof the line, just as in Perl. Blank lines are also allowed. blocksA block is called after a component has finished running. It isgiven the entire output of the component in the $_ variable, and any changesto this variable are reflected in the output of the component. For example,this filter uppercases all of the components output: s/(w+)/U$1/g blocksThis block is executed whenever the component is loaded into memory. It isexecuted before any other block (including an block). Anyvariables declared here remain in existence (and in scope) until thecomponent is flushed from memory or the Perl interpreter running Masonshuts down, whichever comes first. The section is useful forthings like creating database handles or instantiating large, resource-intensive objects. The universe is this big: my $size = calculate_size_of_universe( ); blocksThe cleanup block is executed right before the component exits and is thecounterpart to the block. It is useful if you have created resources-- such as circular references -- that need to be freed. Technically, it is thesame as placing a block at the end of a component. my $resource = get_a_resource( ); ... do something interesting with that resource $resource->dispose; Since cleanup code tends to be put at the end of the component anyway, blocks arent very common. Their chief advantage is thattheir name is cleanup .Cleanup blocks are not executed if the component dies or aborts. blocksThe contents of this block are output exactly as they are, without anyparsing. This if useful if you need to write a component containing textabout Mason. For example: Substitution tags look like this: . blocksThis block is intended for use by component authors for documentationpurposes. Its contents are completely ignored. In the future Mason may dosomething more useful with them. =head1 My Story This is the part where I tell you what thecomponent does. But Id rather tell you a story about my childhood. WhenI was but a child, my mother said to me ... As you can see, theres no reason not to use POD (Perls Plain OldDocumentation markup language) in these blocks, and you can even runperldoc on a component file. and blocksThese two blocks share the same syntax and are used to declare one or morekey/value pairs. The key can contain only letters, numbers, and theunderscore character ( _ ). The value can be any Perl expression whoseresults can fit into a scalar (such as a number, string, reference, or undef ).As in the block, the syntax in these blocks looks like Perl, but it isnot. First, you cannot end a line with a comma or semicolon. Second, thewhole key/value pair must be on a single line.The difference between these two is that the block may containonly official Mason flags, which are used to affect the componentsbehavior. Currently, there is only one flag defined, inherit. This is usedto specify the components parent component. Component inheritance isdiscussed in Chapter 3.The block may contain any keys that you want, as the variablesdefined in this block are not used by Mason but may be used in your code.Its contents are available by calling the objects attr() method and givingthe desired key as the argument. See Chapter 5 for the details. inherit => /some/other/component color => Im so blue size => mucho grande My color: base_comp->attr(color) %>There is one other important difference between flags and attributes: flagsrefer to only the current component, whereas attributes are part of Mason ...

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