It dates back to at least the 1500s. The worm is merely a good choice for the most unlikely creature to strike back at provocation.
If you eat your cake, it's gone, thus you no longer have it. Thus, it's like having the best of both worlds, often by finding a way around choosing between what...