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

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?

Replies are listed 'Best First'.
Re^5: Help me understand this code?
by Laurent_R (Canon) on May 22, 2015 at 06:27 UTC
    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.
      Ok, I get it now. Yeah, I'm still extremely new to Perl but I would never use those variable names in Bash...
Re^5: Help me understand this code?
by AnomalousMonk (Archbishop) on May 22, 2015 at 08:50 UTC

    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:  <%-(-(-(-<

      Gotcha. Thanks!