Template::Plugin::POSIX - TT2 plugin to import POSIX functions


Template-Plugin-POSIX documentation Contained in the Template-Plugin-POSIX distribution.

Index


Code Index:

NAME

Top

Template::Plugin::POSIX - TT2 plugin to import POSIX functions

VERSION

Top

This document describes Template::Plugin::POSIX 0.05 released on 12 March, 2007.

SYNOPSIS

Top

  [% USE POSIX %]

  [% POSIX.log(100) %]
  [% POSIX.rand(1) %]
  [% POSIX.exp(2) %]
  [% POSIX.sprintf("%.0f", 3.5) %]
  [% POSIX.pow(2, 3) %]
  [% POSIX.ceil(3.8) %]
  [% POSIX.floor(3.8) %]
  [% POSIX.sin(3.14) %]
  [% POSIX.cos(0) %]

DESCRIPTION

Top

As a TT programmer, I found it quite inflexible to use the Template Toolkit's presentation language Language due to the very limited vocabulary. So I wrote this little plugin in order to open a window for the template file to the full richness of most POSIX functions, making the Template language a "programming language" in a much more serious sense.

Please keep in mind I just used AUTOLOAD, eval, and Data::Dumper to do the magic here.

If you're looking for even more functions, I suggest you take a look at the Template::Plugin::Perl module which exports the excellent POSIX repertoire.

METHODS

Top

new

Constructor called by the TT2 template system

throw

TT2 exception handling procedure.

TODO

Top

SOURCE CONTROL

Top

You can always get the latest version of the source code from the follow Subversion repository:

http://svn.openfoundry.org/ttposix

There is anonymous access to all.

If you'd like a commit bit, please let me know :)

AUTHOR

Top

Agent Zhang, <agentzh@gmail.com>

COPYRIGHT AND LICENSE

Top

SEE ALSO

Top

Template, Template::Plugin::Perl, Data::Dumper


Template-Plugin-POSIX documentation Contained in the Template-Plugin-POSIX distribution.

package Template::Plugin::POSIX;

use strict;
use warnings;

use POSIX ();
use Data::Dumper;
use Template::Plugin;
use base qw( Template::Plugin );
use vars qw( $AUTOLOAD $VERSION );

our $VERSION = '0.05';

$Data::Dumper::Indent = 0;
*throw = \&Template::Plugin::POSIX::throw;

sub new {
    my ($class, $context, $params) = @_;
    bless {
	    _context => $context,
    }, $class;
}

my $entered = 0;

sub AUTOLOAD {
    my $self = shift;
    my $method = $AUTOLOAD;
    #warn "$method";

    $method =~ s/.*:://;
    return if $method eq 'DESTROY';

    #warn "\@_ = @_\n";
    if ($entered == 1) {
        die("$method not found\n");
    }
    my @args;
    foreach my $arg (@_) {
        my $code = Data::Dumper->Dump([$arg], ['args']);
        $code =~ s/^\s*\$args\s*=\s*(.*);\s*$/$1/s;
        $code =~ s/^\[(.*)\]\s*$/$1/s;
        push @args, $code;
    }
    my $code = "POSIX::$method(".join(',', @args).")";
    #warn "code: $code\n";
    $entered = 1;
    my @retval = eval $code;
    $entered = 0;
    if ($@) {
        $self->throw("POSIX function error: $@");
    }
    if (!@retval) { return (); }
    if (@retval == 1) { $retval[0] }
    else { \@retval };
}

sub throw {
    my $self = shift;
    die (Template::Exception->new('Plugin POSIX', join(', ', @_)));
}

1;
__END__