Wednesday, August 02, 2006

I'm still alive.

I have had a bunch of things going on... incuding my grandma breaking both wrists, my sister getting severe heatstroke and on dialysis for 5 days (a few days as critical!) .. my dad having surgery.... all within the span of about 5 weeks. Talk about crazy. Everybody is pretty much better now, so hopefully we just had all our issues at once.

I've been sort of in hermit mode the past month since YAPC. I even skipped Ruby Meeting!!

I've been doing Perl at my new job and after 6 years of PHP, I find it rather challenging at times as I take different approaches to things probably then the average perl programmer. I'm always saying to my boss.. now I would do it like this or that in ruby/ that the same in perl? ... And in most cases, with a few variations...its about the same and I've been able to function pretty much as normal.

Until today.. whenI suddenly thought... if I had a form like

<input name="data[first]" type="text">
<input name="data[last]" type="text">

will it come into my variable namespace as an array ...err Hash? In PHP, it would come in as an associative array. I was most certainly not looking forward to grabbing a ton of variables from my form (and I know there are larger forms to come in my system!) and passing each one.. blah blah. My head started to hurt. I emailed a friend and he said oh yeah many modules will parse that including ... well thats what I am using (actually Mason) ... I hunt high and low, google and ask the non-serious folks in irc (who weren't too interested in the cause of my headache) ... Hmmm ...

So as my dad always says, if you want it done right, do it yourself. Or in my case, if you can't find it, write it yourself.

sub grab_hash {
my ($hash_ref, $name) = @_;
my %real_hash;
for my $key (keys %$hash_ref) {
if ($key =~ /^($name)\[(\S+)\]$/) {
$real_hash{$2} = $hash_ref->{$key};
return \%real_hash;

I wrote this to sort through the param hash, pull out those items with data[ \S+ ] and build another hash. It works, i pass it the all the vars and it returns me a neat little hash.

I know some of you are thinking I'm insane, and its already there in or some other module, but I can't find it. So if you could please point it out to me, I'd appreciate it. If you have some improvements to my little function, let me know that too!

Weird, in mason the params are supposed to be available as %ARGS .. but when I tried to use \%ARGS to pass to my function the screen was full of a crapload of errors.

Comments: Post a Comment

Links to this post:

Create a Link

<< Home

This page is powered by Blogger. Isn't yours?