10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 12 ■ THE TEMPLATE TOOLKIT 245List variables are set by including the list of values within brackets, [ and ]:[% months = [jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec] %]■Note Commas are used <strong>to</strong> separate the values in the example, but they are not required. A space can beused just the same.These elements are accessed with dot notation, with the first index indicated by 0. Forexample, this code accesses the second element in the months list example just shown:[% month.1 %]Based on that earlier example, the value for [% month.1 %] is feb.While lists or arrays are indicated by brackets, [ and ], hashes are indicated by braces, {and }:[% months = {jan => 'January'feb => 'February'mar => 'March'apr => 'April'}%]Values belonging <strong>to</strong> the keys of the hash are accessed using the dot opera<strong>to</strong>r with hashvariables. For example, the following code accesses the value for the key named mar in theprevious example:[% months.mar %]The value contained in [% months.mar %] is March. You can also use multiple dot opera<strong>to</strong>rs<strong>to</strong> create and retrieve arbitrarily long nested hashes.■Note Variables declared with a leading underscore (_) character are defined as private and will not beused outside the object’s methods.Whereas the variable types just explained are static, meaning they only return valuesexplicitly set or assigned <strong>to</strong> them, dynamic variables can be assigned <strong>to</strong> <strong>Perl</strong> subroutines andobjects, which will then process and return information <strong>to</strong> the variables for use in templateprocessing.Working with VariablesMathematical operations can also be performed on variables when they are being set orretrieved. This example adds the result of two dice being rolled:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!