tags:

views:

62

answers:

1

I've got ORDER BY part of the statement and I have to split it at logical parts

my $sql_part = <<EOH
    IF(some_table.some_field <> 3, 1, 0), 
    some_table.other_field, 
    CASE other_table.third_field WHEN IS NULL THEN 2 ELSE 1 END,
    other_table.third_field
EOH

and, you know, original string doesn't contain newlines and IF's can be nested.

The problem is I don't want to split the stuff inside the nested IF's, function calls.

So simple split /\s*,\s*/, $sql_part won't work.

Is there a regex that can do this, or should I do it just differently?

+3  A: 

There is a regex, but you probably want to just use an SQL Parser: SQL::Statement

ysth
For the benefit of search engines: when a specialised parser as in the given answer is of no use, [`Data::Record`](http://p3rl.org/Data::Record) will do the job.
daxim