Hello Monks.
I was looking at
this perl.com article and I didn't understand how to interpret some of the syntax used:
sub access_check
{
my $user_id = shift;
my @allow_users = @_;
my %quick_allow = map { $_ => 1 } @allow_users;
return $quick_allow{ $user_id };
}
What is the map function doing? It effectively iterates through the array (storing each item in $_) and assigns a value of '1' to each? This results in an overall value of true? Zero would have been treated as false and the user wouldn't have been added to the array of allowed_users? Is this right?
I am also struggling to understand how to dereference code like this:
my %ALL_USER_GROUPS = (
23 => [ qw( g1 g4 ) ],
13 => [ qw( g3 g5 ) ],
);
With so many braces, brackets, parentheses, etc., it quickly becomes difficult to interpret the meaning of the code. Is there a strategy I should routinely employ? How does one name such complex structures? A hash of hashes of arrays? In naming that complex data structure, would I start with the
%ALL_USER_GROUPS hash and then move to the right (looking at the numeric keys) before looking at the anonymous arrays)?
When dereferencing complex data structures, where do I start? How do I know when a reference ends and when to add another curly brace or sigil?
Thanks Monks.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.