Thursday, January 1, 2009

Why I love Perl.....

For a little project, I basically need a quick but simple way of translating things from one format to one of another; the catch? How to do it! I figured, a hash would be a great way to do it, since each of the translations can be quickly processed as translate_from => translate_to. The problem? There's a fair # of keys and values, most hashes I encounter in Perl are written like this:

# modern
my %hash = ( foo => 1, bar => 'string', );
# classic
my %hash = ( 'foo', 1, 'bar', 'string', );

the main value of the arrow operator => is that we get to skip quoting the hashes key... but wtf good is it, if we have to quote each value, because they are all strings in our hash? So I sat and thunk for about 45 seconds and wondered.... wouldn't it be so wonderful, if I could say something like qw/key1 value1 key2 value2 .../ and format it much like the classic style for legibility, but not have to write *any* quotes for the strings?

Attempting it 2 minutes later:

my %hash = qw/foo  1
                bar  string
                ...  ...

It never actually occurred to me before, b/c almost every time I care about hash keys, is w hen I can use a hash as a form of quick dynamically created storage for data, that I will probably want to walk across both the keys and values on later. But since qw/foo bar/ is equivalent to ('foo', 'bar'), it works just perfectly here!

Perl is not the most beautiful language, it's a language for *getting the job done*, and in that spirit, has 1000s of little things that help make things less painful -> like not having to quote every dang gum key value in a hash lol. One thing I also like abut Perl, I can always read my scripts 6++ months later ;-)

I love this tool, lol.

No comments:

Post a Comment