I came across some code that I'm trying to understand. It involves the symbol table. I read about the symbol table in a post Of Symbol Tables and Globs, but I have questions I couldn't find the answers to there.

use Data::Dumper; $hash = { 'user1' => { 'oldpassword' => 0, 'filesize' => '14360', 'logins' => 1 }, 'user2' => { 'oldpassword' => 0, 'filesize' => '1220', 'logins' => 15 }, 'user3' => { 'oldpassword' => 1, 'filesize' => '1780', 'logins' => 7 } }; sub KeysByLogins { my $hash = shift; map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ $hash{$_}->{logins}, $_ ] } keys %$hash; } our @keys = KeysByLogins($hash); foreach my $key (@keys) { print Data::Dumper->Dump([$hash->{$key}], [$key]) . "\n"; # number 1 #print Data::Dumper->Dump([$hash{$key}], [$key]) . "\n"; # number + 2 } print $::{hash}. ' ', $::{keys};
The output was:
$user1 = { 'filesize' => '14360', 'oldpassword' => 0, 'logins' => 1 }; $user3 = { 'filesize' => '1780', 'oldpassword' => 1, 'logins' => 7 }; $user2 = { 'filesize' => '1220', 'oldpassword' => 0, 'logins' => 15 }; *main::hash *main::keys
The line inside the sub map { [ $hash{$_}->{logins}, $_ ] } keys %$hash; isn't using the reference notation $hash->{logins} but is referring to the hash %hash, which wasn't declared. I'm guessing it springs up from the glob(?) in the symbol table (for the duration of the sub).

Then, later on in the print loop, the hash ref is used in line no. 1.

If this line is commented out and line 2 is uncommented, Dumper shows the hash as empty (when accessing the undeclared %hash variable).

Would that be because %hash only springs up in the scope of the subroutine, then is inaccessible outside the sub (in the print routine).

I guess I don't understand the workings of the symbol table. Thanks for any clear explanation of this code snippet.


In reply to 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.