Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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

Replies are listed 'Best First'.
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 (Scribe) 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?
[marto]: there are many, to this day.
[ovedpo15]: @Corion Yes I use the regex you showed me. its very good regex. although now i need to know that substring unit that comma
marto mad corrections to one around 18 months ago
[ovedpo15]: my code checks the value after that comma, if it isn't valid it will remove it. so I would like to remove the substring after that comma meaning getting the string before comma.
[hippo]: Are you sure that Text::CSV_XS isn't already installed at your work? It's such a useful module that it might well be there.
[marto]: pointing out the advantages of cpan modules is well worth in, as both developer and $client/$company benefit greatly
[Tux]: $src =~ m{^(.*),(.*)$/ and $2 !~ $valid and $src = $1;
[Tux]: s,/,},

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-05-27 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?