in reply to Re: Percentages to Fractions
in thread Percentages to Fractions

I feel an urge to explain how &fract works or rather doesn't work, especially since it's the first reply.

The function is complete bogus. It doesn't bother at all which argument it gets (as long as it hasn't been given before). What it does is to populate a hash and then stringify it, returning a string "$used_buckets/$allocated_buckets" (a buckets isn't equal to a value). More on this is in the perldata manual, but here's the relevant excerpt.

If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. -- perldata 5.8.0

The line

%_=(%_,$_[0],$%);
works like
$_{$_[0]} = $%;
except the latter is more efficient and doesn't copy the hash over and over again. I guess why it was written like that was to try to make it non-obvious at a first glance that it uses an accumulating global variable.

The $% variable happens to be a special variable but that's quite irrelevant. It could've been mostly any scalar and I can only assume that $% was chosen because of the obfuscation effect. However, as I pointed out here, the hash algorithm sometimes share values and hence sometimes just uses one bucket, making the function not work as intended.

The ~~ is actually the unary operator ~ applied twice. ~ is bitwise negation, and if you doubly negate you get the original back. The side-effect is that you put the operand in scalar context, which is what's used here.

ihb