This code was obviously written without strict and should therefore only serve as an example as to why strict should always be used. Anyways, on to the analysis:

The code is obviously supposed to sort its output by number of logins, but if you run it several times, you'll see it doesn't work!

First clue as to the error: If you turn on warnings and strict - as one always should, even when trying to understand legacy code! - and then declare our ($hash, %hash); at the top of the code, you'll see warnings like this: "Use of uninitialized value in numeric comparison (<=>) at ..."

sub KeysByLogins is the only place where %hash is referenced, and there it is only read from, not written to, that's why it remains empty.

If you break the Schwartzian Transform happening in that sub into pieces, you can see that happening:

sub KeysByLogins { my $hash = shift; my @temp = map { [ $hash{$_}->{logins}, $_ ] } keys %$hash; print Dumper(\@temp); return map { $_->[1] } sort { $a->[0] <=> $b->[0] } @temp; }

The first element in each pair is always undef, because %hash is empty.

So it's really not got much to do with the symbol table, just a single typo bug caused by not using strict.


In reply to Re: Question about the symbol table by Anonymous Monk
in thread Question about the symbol table by Cristoforo

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.