in reply to Throw compilation error(or warning) if duplicate keys are present in hash

There is no duplicate key at compile-time — the list is only created and assigned to the hash at run-time — so the answer to your literal question is "no".

However, it is possible to write an opcode checker that detects the posted code pattern at compile-time. (For example, module autovivification uses an opcode checker to not only examine opcodes, but to replace them.) This approach would probably be better to add a *run-time* warning.

A better approach to detecting the situation at compile-time would be to write a Perl::Critic rule to look for the code pattern you posted.

  • Comment on Re: Throw compilation error(or warning) if duplicate keys are present in hash

Replies are listed 'Best First'.
Re^2: Throw compilation error(or warning) if duplicate keys are present in hash
by I_love_perl (Novice) on Nov 24, 2011 at 09:18 UTC

    Hi Ikegami, Thanks for your quick response.

    I've tried using Perl::Critic (http://perlcritic.com/), but it is not throwing up any warning with respect to the re-occurrence of the hash key. Correct me if I'm doing something wrong in using Perl::Critic

      I didn't say a rule already existed to check this. Quite the opposite, I assumed there wasn't one and told you you'd have to actually write the rule.