WebMake: Documentation: The <{perl}> Directives

WebMake Documentation (version 2.3) | [ Back | Forward | Index | All In One ]

# The <{perl}> Directives Arbitrary perl code can be executed using this directive. It works like perl's `eval` command; the return value from the perl block is inserted into the file, so a perl code block like this:
    	<{perl
    	  $_ = '';
    	  for my $fruit (qw(apples oranges pears)) {
    	    $_ .= " ".$fruit;
    	  }
    	  $_;
    	}>
will be replaced with the string " apples oranges pears". Note that the `$_` variable is declared as local when you enter the perl block, you don't have to do this yourself. If you don't like the eval style, you can use a more PHP/JSP/ASP-like construct using the `perlout` directive, which replaces the perl code text with anything that the perl code prints on the default output filehandle, like so:
    	<{perlout
    	  for my $fruit (qw(apples oranges pears)) {
    	    print " ", $fruit;
    	  }
    	}>
Note that this is not STDOUT, it's a local filehandle called `$outhandle`. It is selected as the default output handle, however, so just `print` without a filehandle name will work. Also, it should be noted that `perl` is a little more efficient than `perlout`, so if you're going all-out for speed, you should use that. <{perl}> sections found at the top level of the WebMake file will be evaluated during the file-parsing pass, as they are found. <{perl}> sections embedded inside content chunks or other tagged blocks will be evaluated only once they are referenced. Perl code can access content variables and URLs using the library functions provided. The library functions are available both as normal perl functions in the default `main` package, or, if you want to write thread-safe or mod_perl-safe perl code, as methods on the `$self` object. The `$self` object is available as a local variable in the perl code block. A good example of perl use inside a WebMake file can be found in the `news_site.wmk` file in the examples directory.

WebMake Documentation (version 2.3) | [ Back | Forward | Index | All In One ]