Hi,
I pondered elsewhere over the reason that the following one-liner produced a different result each time it was run:
C:\>perl -MPDL -E "srand(3); say rand();"
0.422170424674736
C:\>perl -MPDL -E "srand(3); say rand();"
0.000277820349769087
It was explained to me that the PDL module exports its own srand() function - a function that silently clobbers perl's srand() function, and does not seed perl's pseudo-random generator.
I was surprised, firstly, that PDL would do such a thing. Why don't they call their srand() function something like (eg) pdl_srand() ?
Then, secondly, I was surprised that perl would allow its srand() function to be clobbered in this way, without so much as a whimper.
Not even the loading of strict.pm or warnings.pm leads to a warning that srand() is no longer what I think it is.
Thirdly, I wondered how one might protect oneself from such a trap.
All I could think of is to replace the
srand(3) call in my one-liner with
CORE::srand(3) .... is there another way ?
It's going to get rather tedious If one starts sticking a "CORE::" prefix in front of every perl core function call, just in case one of the loaded modules has overridden that particular perl core function.
And I'm not seriously suggesting that I'm about to start doing that. This is the first time I've ever struck such an issue, so I reckon it's a very rare scenario.
What do others think about the points I've just raised ?
For the purpose of playing around with this I created the following module (OverRider.pm):
package OverRider;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(sqrt);
our $VERSION = '0.01';
sub sqrt {
return sprintf "%.7g", $_[0] ** 0.5;
}
1;
Then I placed that file in one of my @INC directories and ran:
D:\>perl -Mstrict -Mwarnings -MOverRider -le "print sqrt(2); print COR
+E::sqrt(2);"
1.414214
1.4142135623731
Cheers,
Rob
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.