Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: On creating hash whose keys aren't case-sensitive

by CountZero (Bishop)
on Feb 18, 2012 at 12:50 UTC ( #954748=note: print w/ replies, xml ) Need Help??


in reply to On creating hash whose keys aren't case-sensitive

Following the maxim of "be strict with your output, but liberal with your input", it is a Best Practice™ to always check, sanitize and normalize your input.

And just for the fun of it: your _lc rewritten into one regex:

use Modern::Perl; sub _lc { my $key = shift; my $success = $key =~ s/^-?([^-_]*)[-_]?([^-_]*)$/$2?lc"$1_$2":lc$ +1/e; return $key if $success; } print _lc($_) while (<DATA>); __DATA__ content-length content_length -content-length -content_length Content-Length Content_Length -Content-Length -Content_Length changed Changed -changed -Changed Too-many_dashes-Or-underscores
This sub returns undef (indicating an error) if you give it a string with more than one internal dash or underscore.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics


Comment on Re: On creating hash whose keys aren't case-sensitive
Select or Download Code
Re^2: On creating hash whose keys aren't case-sensitive
by tobyink (Abbot) on Feb 18, 2012 at 13:12 UTC

    This sub returns undef (indicating an error) if you give it a string with more than one internal dash or underscore.

    Like the Access-Control-Allow-Origin header?

      Absolutely!

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
Re^2: On creating hash whose keys aren't case-sensitive
by anazawa (Beadle) on Feb 18, 2012 at 15:20 UTC
    Thanks for your suggestion. Although I didn't understand why your code worked so, I confirmed your sub worked in an expected way. I didn't consider whether to normalize users' inputs. In my case, "be strict with your output" points out the following:
    # Header.pm sub set_header { $header_ref->{ $key } = $value; }
    "(be) liberal with your input" does the following:
    # plugins/foo set_header( $main::header_ref, '-status' => '304 Not Modified' );
    Your comment meant a lot to me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954748]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-08-31 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls