Aighearach has asked for the wisdom of the Perl Monks concerning the following question:

DBI doesn't seem to support array types (or maybe I just missed it) and PostgeSQL does. So, I wrote this routine to parse them out.

The way the postgresql arrays work is that the array is serialized into a string with comma delimited values, and enclosed in braces {}. text types are quoted in double quotes, and numbers are left bare. So to insert a list of lists, your SQL might look like this:

INSERT INTO tablename VALUES ( '{ {"this is 0,0", "this is 0,1"}, {"1,0"} }' )

So my question is, how would I go about making the following code more efficient? I already have some cute hacks, like the s/^{// and chop that work because of the unusual situation of having highly controlled input data. This is one of those cases where run speed is way more important than readability or maintainability because A) once finished there are no features to add, the problem domain will be saturated and B) I'm going to port it to PL/Perl and run it as a stored procedure in the database, so the users of the routine won't be able to mess with it directly anyway.

Here is the code:

sub de_postgresify_string { my $string = shift; return undef unless defined $string and length $string; $string =~ s/^\{// and chop $string; my @array = map { s/^\s+//; s/\s+$//; $_ } split /,/, $string; foreach my $chunk ( @array ) { if ( $chunk =~ /^\{/ ) { $chunk = [ de_postgresify_string( $chunk ) ]; next; } $chunk =~ s/^\"// and chop $chunk; } return @array; }

--
Snazzy tagline here