Hash::Key::Quote - Utility routines related to quoting of hash keys


This document describes version 0.001 of Hash::Key::Quote (from Perl distribution Hash-Key-Quote), released on 2019-07-10.



should_quote_hash_key($str) => bool

Return true if $str should be quoted as a hash key when placed before the fat comma (=>) operator. According to the perlop documentation:

 The "=>" operator (sometimes pronounced "fat comma") is a synonym for the comma
 except that it causes a word on its left to be interpreted as a string if it
 begins with a letter or underscore and is composed only of letters, digits and
 underscores. This includes operands that might otherwise be interpreted as
 operators, constants, single number v-strings or function calls. If in doubt
 about this behavior, the left operand can be quoted explicitly.

This means strings like "and" or "v1" need not be quoted.

But there are several other cases where a string needs not be quoted. For example, numbers except in these cases:

 012        # perl will interpret it as a positive octal literal
 -012       # perl will interpret it as a negative octal literal
 1_000_000  # perl will strip the underscores from number
 -1_000_000 # ditto
 1_0e10     # ditto
 -1_0e10    # ditto
 +123       # perl will strip the + sign
 -+123      # ditto
 1e2        # perl will normalize it to "100"
 1_00a      # not a valid number, must be quoted

Another example is non-number string that begins with a dash and followed only by letters/numbers/underscores, e.g. -foo.

For simplicity, you should probably just always quote. But if you only want to quote when necessary, this routine can help you.


Please visit the project's homepage at


Source repository is at


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.



Data::Dump from which this code is extracted from.


perlancar <>


This software is copyright (c) 2019 by

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.