1#============================================================= -*-Perl-*- 2# 3# Template::Plugin::Math 4# 5# DESCRIPTION 6# Plugin implementing numerous mathematical functions. 7# 8# AUTHORS 9# Andy Wardley <abw@wardley.org> 10# 11# COPYRIGHT 12# Copyright (C) 2002-2007 Andy Wardley. All Rights Reserved. 13# 14# This module is free software; you can redistribute it and/or 15# modify it under the same terms as Perl itself. 16# 17#============================================================================ 18 19package Template::Plugin::Math; 20 21use strict; 22use warnings; 23use base 'Template::Plugin'; 24 25our $VERSION = 1.16; 26our $AUTOLOAD; 27 28 29#------------------------------------------------------------------------ 30# new($context, \%config) 31# 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#------------------------------------------------------------------------ 37 38sub new { 39 my ($class, $context, $config) = @_; 40 $config ||= { }; 41 42 bless { 43 %$config, 44 }, $class; 45} 46 47sub abs { shift; CORE::abs($_[0]); } 48sub atan2 { shift; CORE::atan2($_[0], $_[1]); } # prototyped (ugg) 49sub cos { shift; CORE::cos($_[0]); } 50sub exp { shift; CORE::exp($_[0]); } 51sub hex { shift; CORE::hex($_[0]); } 52sub int { shift; CORE::int($_[0]); } 53sub log { shift; CORE::log($_[0]); } 54sub oct { shift; CORE::oct($_[0]); } 55sub rand { shift; CORE::rand($_[0]); } 56sub sin { shift; CORE::sin($_[0]); } 57sub sqrt { shift; CORE::sqrt($_[0]); } 58sub srand { shift; CORE::srand($_[0]); } 59 60# Use the Math::TrulyRandom module 61# XXX This is *sloooooooowwwwwwww* 62sub truly_random { 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(); 67} 68 69eval { 70 require Math::Trig; 71 no strict qw(refs); 72 for my $trig_func (@Math::Trig::EXPORT) { 73 my $sub = Math::Trig->can($trig_func); 74 *{$trig_func} = sub { shift; &$sub(@_) }; 75 } 76}; 77 78# To catch errors from a missing Math::Trig 79sub AUTOLOAD { return; } 80 811; 82 83__END__ 84 85=head1 NAME 86 87Template::Plugin::Math - Plugin providing mathematical functions 88 89=head1 SYNOPSIS 90 91 [% USE Math %] 92 93 [% Math.sqrt(9) %] 94 95=head1 DESCRIPTION 96 97The Math plugin provides numerous mathematical functions for use 98within templates. 99 100=head1 METHODS 101 102C<Template::Plugin::Math> makes available the following functions from 103the Perl core: 104 105=over 4 106 107=item abs 108 109=item atan2 110 111=item cos 112 113=item exp 114 115=item hex 116 117=item int 118 119=item log 120 121=item oct 122 123=item rand 124 125=item sin 126 127=item sqrt 128 129=item srand 130 131=back 132 133In addition, if the L<Math::Trig> module can be loaded, the following 134functions are also available: 135 136=over 4 137 138=item pi 139 140=item tan 141 142=item csc 143 144=item cosec 145 146=item sec 147 148=item cot 149 150=item cotan 151 152=item asin 153 154=item acos 155 156=item atan 157 158=item acsc 159 160=item acosec 161 162=item asec 163 164=item acot 165 166=item acotan 167 168=item sinh 169 170=item cosh 171 172=item tanh 173 174=item csch 175 176=item cosech 177 178=item sech 179 180=item coth 181 182=item cotanh 183 184=item asinh 185 186=item acosh 187 188=item atanh 189 190=item acsch 191 192=item acosech 193 194=item asech 195 196=item acoth 197 198=item acotanh 199 200=item rad2deg 201 202=item rad2grad 203 204=item deg2rad 205 206=item deg2grad 207 208=item grad2rad 209 210=item grad2deg 211 212=back 213 214If the L<Math::TrulyRandom> module is available, and you've got the time 215to wait, the C<truly_random_number> method is available: 216 217 [% Math.truly_random_number %] 218 219=head1 AUTHOR 220 221Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/> 222 223=head1 COPYRIGHT 224 225Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. 226 227This module is free software; you can redistribute it and/or 228modify it under the same terms as Perl itself. 229 230=head1 SEE ALSO 231 232L<Template::Plugin> 233 234=cut 235 236# Local Variables: 237# mode: perl 238# perl-indent-level: 4 239# indent-tabs-mode: nil 240# End: 241# 242# vim: expandtab shiftwidth=4: 243