1 #============================================================= -*-Perl-*-
3 # Template::Plugin::Math
6 # Plugin implementing numerous mathematical functions.
9 # Andy Wardley <abw@wardley.org>
12 # Copyright (C) 2002-2007 Andy Wardley. All Rights Reserved.
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
17 #============================================================================
19 package Template::Plugin::Math;
23 use base 'Template::Plugin';
29 #------------------------------------------------------------------------
30 # new($context, \%config)
32 # This constructor method creates a simple, empty object to act as a
33 # receiver for future object calls. No doubt there are many interesting
34 # configuration options that might be passed, but I'll leave that for
35 # someone more knowledgable in these areas to contribute...
36 #------------------------------------------------------------------------
39 my ($class, $context, $config) = @_;
47 sub abs { shift; CORE::abs($_[0]); }
48 sub atan2 { shift; CORE::atan2($_[0], $_[1]); } # prototyped (ugg)
49 sub cos { shift; CORE::cos($_[0]); }
50 sub exp { shift; CORE::exp($_[0]); }
51 sub hex { shift; CORE::hex($_[0]); }
52 sub int { shift; CORE::int($_[0]); }
53 sub log { shift; CORE::log($_[0]); }
54 sub oct { shift; CORE::oct($_[0]); }
55 sub rand { shift; CORE::rand($_[0]); }
56 sub sin { shift; CORE::sin($_[0]); }
57 sub sqrt { shift; CORE::sqrt($_[0]); }
58 sub srand { shift; CORE::srand($_[0]); }
60 # Use the Math::TrulyRandom module
61 # XXX This is *sloooooooowwwwwwww*
63 eval { require Math::TrulyRandom; }
64 or die(Template::Exception->new("plugin",
65 "Can't load Math::TrulyRandom"));
66 return Math::TrulyRandom::truly_random_value();
72 for my $trig_func (@Math::Trig::EXPORT) {
73 my $sub = Math::Trig->can($trig_func);
74 *{$trig_func} = sub { shift; &$sub(@_) };
78 # To catch errors from a missing Math::Trig
79 sub AUTOLOAD { return; }
87 Template::Plugin::Math - Plugin providing mathematical functions
97 The Math plugin provides numerous mathematical functions for use
102 C<Template::Plugin::Math> makes available the following functions from
133 In addition, if the L<Math::Trig> module can be loaded, the following
134 functions are also available:
214 If the L<Math::TrulyRandom> module is available, and you've got the time
215 to wait, the C<truly_random_number> method is available:
217 [% Math.truly_random_number %]
221 Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
225 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
227 This module is free software; you can redistribute it and/or
228 modify it under the same terms as Perl itself.
238 # perl-indent-level: 4
239 # indent-tabs-mode: nil
242 # vim: expandtab shiftwidth=4: