I thought there was a tie module for this but a quick scan of the first couple of hundred didn't find it. So here's one.

As Limbic~Region point's out, I was looking in the wrong namespace. As well as Hash::Case::Preserve, there is also Hash::Case::Lower. The difference between the two (besides the obvious) is that the former uses two hashes internally. One for allowing the case-insensitive matching, the other for preserving the original case of the keys for when iterating with keys or each.

The also showed me that I forgot a couple of bits below. (Now added.)

#! perl -slw use strict; package Tie::Hashi; use Tie::Hash; our @ISA = 'Tie::ExtraHash'; sub TIEHASH{ bless [{}], $_[ 0 ]; } sub STORE { $_[ 0 ][ 0 ]{ lc $_[ 1 ] } = $_[ 2 ]; } sub FETCH { $_[ 0 ][ 0 ]{ lc $_[ 1 ] }; } sub EXISTS{ exists $_[ 0 ][ 0 ]{ lc $_[ 1 ] }; } sub DELETE{ delete $_[ 0 ][ 0 ]{ lc $_[ 1 ] }; } 1; package main; tie my %hashi, 'Tie::Hashi'; $hashi{ TheKey } = 12345; print $hashi{ tHeKeY }; print keys %hashi, $/, values %hashi; __END__ P:\test>tiehashi.pl 12345 thekey 12345

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re: case-insensitive hash keys by BrowserUk
in thread case-insensitive hash keys by ManFromNeptune

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.