This may or may not work for your purposes, but I ran into a similar issue when trying to dynamically set last names.
I knew what it needed to be at spawn time and it varied by spawn only so I'm not changing last names while the mob is alive and active.
If you look at mod_functions.cpp you can just put something in NPC::mod_prespawn and manipulate the NPC in a number of ways before it's actually spawned which, at the time, was the only place you could change the last name outside of the database.
I'm using it to throw random 'boss' mobs into spawns that have been killed 1 or more times and the last name represents a difficulty meter.
Code:
strn0cpy(lastname, bosstag.c_str(), sizeof(lastname));
TempName("DYNBOSS");
From there a perl script takes over.