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


In reply to Re: Re: Percentages to Fractions by ihb
in thread Percentages to Fractions by David Caughell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.