in reply to Perl and PostgreSQL regex

I would recommend that you first try this from the psql prompt, and only then try to put it in your code. I will also warn you that you are about to experience leaning toothpick syndrome.

Suppose you want to match the pattern /branch.\d+/. Well for postgres you need to pass that in as a string and every \ needs to be doubled to escape them because \ already has meaning. So you get 'branch.\\d+'. But in Perl \ is special, so to pass that into postgres you have to write 'branch.\\\\d+'.

If you are very sure that $root has no escape characters in it, you can write something like

$sth_get_branch_list = $dbh->prepare(qq( SELECT branch FROM postgres_database WHERE tre +e = 'first_tree' AND branch ~ '$root.\\\\d+'; ));
and it should work. But be warned that interpolating variables directly into SQL is a serious security risk because it leads to the possibility of SQL injection attacks. So you are better off with something like:
$sth_get_branch_list = $dbh->prepare(q( SELECT branch FROM postgres_database WHERE tre +e = 'first_tree' AND branch ~ ? || '.\\\\d+'; ));
and then passing $root into execute. This will still not do the right thing if $root has regular expression metacharacters in it, but at least it isn't a major security risk.