Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning for some usages of Math.rand [rt.cpan.org #70957] #155

Open
atoomic opened this issue Oct 5, 2018 · 1 comment
Open

Warning for some usages of Math.rand [rt.cpan.org #70957] #155

atoomic opened this issue Oct 5, 2018 · 1 comment

Comments

@atoomic
Copy link
Collaborator

atoomic commented Oct 5, 2018

Migrated from rt.cpan.org#70957 (status was 'open')

Requestors:

Attachments:

From srezic@cpan.org on 2011-09-14 07:39:07:
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

From srezic@cpan.org on 2011-09-14 07:42:20:

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

@toddr
Copy link
Collaborator

toddr commented Oct 5, 2018

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';

toddr added a commit that referenced this issue Oct 8, 2018
Resolves GH #155: Calling this without an arg caused the following error:
Use of uninitialized value $_[0] in rand at
/usr/lib/perl5/Template/Plugin/Math.pm line 55.

The issue is fixed and tests are added to confirm this works correctly in
the future.
toddr added a commit that referenced this issue Oct 8, 2018
Resolves GH #155: Calling this without an arg caused the following error:
Use of uninitialized value $_[0] in rand at
/usr/lib/perl5/Template/Plugin/Math.pm line 55.

The issue is fixed and tests are added to confirm this works correctly in
the future.
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Nov 24, 2018
Upstream changes:
Version 2.28 - 11th October 2018
#------------------------------------------------------------------------

* Add and enable Travis CI to track GitHub Pull Requests

* Template is now using GitHub as the official Bug Tracker

* Nicolas R. fixed a circular reference in Template::Plugin::Filter
  abw/Template2#152

* Nicolas R. adjusted group regexes to not be greedy
  abw/Template2#94

* Nicolas R. added unit tests to cover regression from RT 91172
  abw/Template2#122

* Nicolas R. added support for template files having mtime=0
  abw/Template2#102

* Todd Rinaldo fixed rand calls with no args in Math plugin
  abw/Template2#155

* Todd Rinaldo corrected ttree 2.22 logic change
  abw/Template2#148

* Todd Rinaldo turned off automated testing for tests using optional modules
  abw/Template2#156

* Nicolas R. adjusted unit tests to not force Stash::XS

* Nicolas R. added a pre allocated buffer in Stash.xs to avoid malloc/free
  abw/Template2#82

* Nicolas R. optmized Template::Parser by avoiding a dummy sub
  abw/Template2#83

* Nicolas R. optimized Template:Directive by using index
  abw/Template2#84

* Nicolas R. adjust _dotop logic in Stash for perl 5.28 and earlier
  abw/Template2#81

* Todd Rinaldo documented VMethod method called 'item'
  abw/Template2#90

* Nicolas R. adjusted t/filter.t after recent switch to RFC3986
  abw/Template2#179

* Nicolas R. fixed warnings from t/cgi.t
  abw/Template2#178

* Ivan Krylov added STRICT option to ttree
  abw/Template2#81

* Kent Fredric fixed relative path handling in templates on Perl 5.26+
  abw/Template2#80

* Tom Delmas fixed some typo from documentation
  abw/Template2#76

* Matthew Somerville switched uri/url to use RFC3986
  updated the documentation to match the history.
  abw/Template2#35

* Sebastien Deseille used remove_tree helper to remove directories
  abw/Template2#67

* Nick Hibma - Add Sortkeys to DUMPER_ARGS
  abw/Template2#64

* E. Choroba added a warn on duplicate block name
  abw/Template2#61

* Jason Lewis fixed some typo in ttree.pod
  abw/Template2#58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants