barefootcoder/Data-Random

wish: rand_chars_string, rand_words_string

Closed this issue · 2 comments

Hello,

I found find it really useful of the random character and word functions could be
returned as strings. For the words, I think it makes sense to join them with a space, and
for the characters, to just join them directly. I propose

rand_chars_string
rand_words_string

Or.../Better/ could just detect that the routine is being called in scalar context?

So:

$string = rand_chars()
and
@array = rand_chars()

...both could do the Right Thing? That would be nice.

Thanks,

Mark

I see your point, and I think your idea is a good one. I'd certainly prefer that we just use the context rather than creating all new functions. The problem is, of course, what happens to anyone who is relying on the current behavior? First of all, we have to figure out what the current behavior is ...

For rand_chars, it seems like what it thinks it's doing is returning an arrayref instead of an array. However, it has a bug, and what it's actually doing is is returning a reference to the first character in the array (at least I assume it's the first; impossible to tell, given that it's random ;-> ). Therefore, this:

$string = rand_chars(set => 'alpha', size => 4);  # returns xjwi or somesuch

is rational, and the previous(ly advertised) behavior is achieved easily enough as well:

$arrayref = [ rand_chars(set => 'alpha', size => 4) ];  # returns [ qw< x j w i > ] or somesuch

For rand_words, it's tougher. It also thinks it's returning an arrayref instead of an array if called in scalar context, and (perhaps unfortunately), in this case, it's right. Furthermore, doing something like this:

$arrayref = [ rand_words(size => 4) ];

isn't as good as it is for rand_chars, where it would work pretty much exaclty the same as it would if I were to fix the bug. But, in this case, it would actually end up making a completely unnecessary copy of the array, which might be problematic when a large number of random words are requested.

Still, that seems pretty unlikely, overall. My initial reaction would be to make the change for rand_chars immediately (after all, it's broken anyway), and perhaps stick in a deprecation warning for calling rand_words in a scalar context and then change it next time around if no one whinges about it. But let me think on it a bit.

Thanks to a patch from @neilbowers, this is now available (finally). Once you can access version 0.10 from CPAN, see if that doesn't fix the problem. I'm going to close out this issue now.