Sub

### Index Up << >>

## Sub

Defines a catalog subroutine for use by the `[perl][/perl]` or [mvasp] embedded perl languages. Use the ``here document'' capability of MiniVend configuration files to make it easy to define:

        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 .= '&lt/TABLE>';
            return $out;
        }
        EOF

As with Perl ``here documents'', the EOF (or other end marker) must be the ONLY thing on the line, with no leading or trailing white space. Do not append a semicolon to the marker.

The above would be called with:

        [perl]
            my $cart = $Carts->{main};
            return sort_cart($cart);
        [/perl]

and will display an HTML table of the items in the current shopping cart, sorted by the quantity. Syntax errors will be reported at catalog startup time.

Catalog subroutines may not perform unsafe operations -- the Safe.pm module enforces this unless global operations are allowed for the catalog. See `AllowGlobal`.

### Index Up << >>