Skip Menu |

This queue is for tickets about the Template-Toolkit CPAN distribution.

Report information
The Basics
Id: 70957
Status: resolved
Priority: 0/
Queue: Template-Toolkit

People
Owner: Nobody in particular
Requestors: SREZIC [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 2.22
Fixed in: (no value)



Subject: Warning for some usages of Math.rand
The perl core functions rand and srand may be called without arguments. When using the same functions without arguments in the TT2 plugin Math, a warning may appear: $ perl -l -w -MTemplate -e '$t=Template->new;$t->process(\"[% USE Math; Math.rand %]",{})or die $t->error' Use of uninitialized value $_[0] in rand at /usr/lib/perl5/Template/Plugin/Math.pm line 55. 0.509199773267941 Probably the plugin methods rand() and srand() should check if there's anything in @_ and call the core function differently. Regards, Slaven
On 2011-09-14 03:39:07, SREZIC wrote: Show quoted text
> The perl core functions rand and srand may be called without arguments. > When using the same functions without arguments in the TT2 plugin Math, > a warning may appear.
[...] Attached a patch to fix the problem, together with a new test script (the test case did not fit in the existing t/math.t). Regards, Slaven
Subject: tt2-math-rand.diff
diff --git c/MANIFEST w/MANIFEST index ea00da1..1d2177b 100644 --- c/MANIFEST +++ w/MANIFEST @@ -126,6 +126,7 @@ t/lib/Template/Plugin/Simple.pm t/list.t t/macro.t t/math.t +t/math2.t t/object.t t/output.t t/parser.t diff --git c/lib/Template/Plugin/Math.pm w/lib/Template/Plugin/Math.pm index ff40004..5482e91 100644 --- c/lib/Template/Plugin/Math.pm +++ w/lib/Template/Plugin/Math.pm @@ -52,10 +52,10 @@ sub hex { shift; CORE::hex($_[0]); } sub int { shift; CORE::int($_[0]); } sub log { shift; CORE::log($_[0]); } sub oct { shift; CORE::oct($_[0]); } -sub rand { shift; CORE::rand($_[0]); } +sub rand { shift; @_ ? CORE::rand($_[0]) : CORE::rand(); } sub sin { shift; CORE::sin($_[0]); } sub sqrt { shift; CORE::sqrt($_[0]); } -sub srand { shift; CORE::srand($_[0]); } +sub srand { shift; @_ ? CORE::srand($_[0]) : CORE::srand(); } # Use the Math::TrulyRandom module # XXX This is *sloooooooowwwwwwww* diff --git c/t/math2.t w/t/math2.t new file mode 100644 index 0000000..9d7bfa4 --- /dev/null +++ w/t/math2.t @@ -0,0 +1,19 @@ +use strict; +use Test::More; +use Template; + +plan tests => 1; + +my @warnings; +local $SIG{__WARN__} = sub { push @warnings, @_ }; +my $t = Template->new; +my $out; +$t->process(\<<EOF, {}, \$out) or die $t->error; +[% USE Math -%] +rand with arg: [% Math.rand(1000000) %] +rand without arg: [% Math.rand %] +srand with arg: [% Math.srand(1000000) %] +srand without arg: [% Math.srand %] +EOF +#diag $out; +is_deeply \@warnings, [], 'No warnings when calling rand/srand without arg';
Ticket migrated to github as https://github.com/abw/Template2/issues/155