in reply to strict and hashrefs

Since hash keys are strings, the question is really how can you avoid one-off errors when a string appears multiple times in a piece of code. Two solutions occur to me:

  1. At the head of your code you could predeclare all your desired keys, either in an array or a series of scalars. In that way, you could directly harness strict to keep your keys in line.
  2. A solution I think is a bit more elegant would be using constants to assign your allowed keys at start. Something like:

    #!/usr/bin/perl use strict; use warnings; use constant {KEY_1 => 0, KEY_2 => 1}; my %hash = (KEY_1() => 5, KEY_2, 7); print $hash{KEY_1()}; print @hash{KEY_1, KEY_2};

    Note that constants are really subroutines (Thanks for the CB help tye), so trying to access them using $hash{KEY_1} will fail, though you could build in a little resilience by making the value of KEY_1 equal to 'KEY_1'.