in reply to Re: Help me understand this code?
in thread Help me understand this code?

SO I've been reading perlre and perlop and I get most of it now. But I'm still confused on the $m{$1} and $m{$_} parts. What do these do?

Replies are listed 'Best First'.
Re^3: Help me understand this code?
by Anonymous Monk on May 21, 2015 at 22:44 UTC

    %m is a hash mapping the 3 letter month to a month number.

    $m{$_} is where elements are being assigned inside a for loop.

    $m{$1} is dereferencing a particular value.

    -Greg
      I don't see a %m, I only see $m? Where is it being defined as a hash? Or is that syntax to initialize a hash: $var{$item} ? Last question too, what do the pounds in $t#$t#$t do?
        The hash is being defined in this line:
        $m{$_} = ++$i for qw[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec];
        After having executed this statement (or maybe I should say this loop), the following structure exists in memory:
        ( Jan => 1, Feb => 2, ...)
        Please beware that this code is fairly sloppy, with no declaration of variables, it is not an example to be followed.

        Further to Laurent_R's post: This is known as "autovivification" (see perlglossary) and is occurring with all the variables in the OPed code except $a and $b, which are pre-existing Perl special variables (see perlvar). Only package-global variables are created in this way, so this practice is officially Frowned Upon and is precluded by using strictures (see strict). In almost all cases, it's better to pre-declare lexical variables with my.

        ... what do the pounds in $t#$t#$t do?

        They match against literal  '#' characters in a string. They have no special function in a regex unless the  /x regex modifier is used (see Modifiers), in which case  # becomes a comment metacharacter/operator and must be escaped in some way to match against a literal character.


        Give a man a fish:  <%-(-(-(-<