What am I doing wrong?
Using Perl.
Smart arse comments aside, it looks like you haven't terminated the string constant for the database query.
The other queries have a " at the end of the query where the rest of the code continues.
The method call to $db->SelectARef("SELECT f.* blah blah blah", x, y, z); looks to be a parameterized query, where you'd need to specify x, y, and z variables that get replaced in the query at the places marked by the question marks.