Sub — define Perl functions for use within a catalog
Define a catalog subroutine for use within [perl]
, [mvasp]
, or
embedded Perl languages.
The use of "here document" syntax in Interchange makes subroutine definitions visually convenient.
Example: Defining a subroutine
Sub <<EOF sub sort_cart_by_quantity { my($items) = @_; $items = $Items if ! $items; my $out = '<table border="1">'; @$items = sort { $a->{quantity} <=> $b->{quantity} } @$items; foreach $item (@$items) { my $code = $item->{code}; $out .= '<tr><td>'; $out .= $code; $out .= '</td><td>'; $out .= $Tag->data('products', 'name', $code); $out .= '</td><td>'; $out .= $Tag->data('products', 'price', $code); $out .= '</td></tr>'; } $out .= '</table>'; return $out; } EOF
The above code would be called from an Interchange page in the following way:
Items, sorted by quantity: [perl tables=products subs='sort_cart_by_quantity'] my $cart = $Carts->{main}; return sort_cart_by_quantity($cart); [/perl]
As with Perl "here documents," the "EOF
" (or arbitrarily
named end marker) must be the only thing on the line,
without leading or trailing white space. Do not append a semicolon to the
marker (as you would in Perl).
Catalog subroutines may not perform unsafe operations. The
Safe
module enforces this unless global operations are allowed for the
catalog. See safe glossary entry for more information.