| 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-2022 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 | |||
| 19 | package Template::Plugin::Math; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use base 'Template::Plugin'; | ||
| 24 | |||
| 25 | our $VERSION = '3.105'; | ||
| 26 | our $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 | |||
| 38 | sub new { | ||
| 39 | my ($class, $context, $config) = @_; | ||
| 40 | $config ||= { }; | ||
| 41 | |||
| 42 | bless { | ||
| 43 | %$config, | ||
| 44 | }, $class; | ||
| 45 | } | ||
| 46 | |||
| 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]) : CORE::rand(); } | ||
| 56 | sub sin { shift; CORE::sin($_[0]); } | ||
| 57 | sub sqrt { shift; CORE::sqrt($_[0]); } | ||
| 58 | sub srand { shift; @_ ? CORE::srand($_[0]) : CORE::srand(); } | ||
| 59 | |||
| 60 | # Use the Math::TrulyRandom module | ||
| 61 | # XXX This is *sloooooooowwwwwwww* | ||
| 62 | sub 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 | |||
| 69 | eval { | ||
| 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 | ||
| 79 | sub AUTOLOAD { return; } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | __END__ |