Check to see if your dbengine supports user-defined/ pre-defined functionality. MySQL has limited support for engine-side functions. Then, you should be able to use any SQL line to indicate which functions you wish to execute on which data sets. This solution is best if you're really dealing with a function that's used alot. Keep the function in the engine rather than in a Perl sub. (Plus, it'll make other perl scripts for the engine easier to write and you won't have to worry about strange SQL arrangements in your scripts.)