in reply to how do I write directly to a file

First, you close COLOR just after writing the default values from @colors. You should close it after the second while loop.

Second, if you want to force your data to be written at once, you need to set the autoflush, by means of the $| variable. As $| acts on STDOUT by default, you need to select your COLORS before, and select STDOUT back:

select +(select(COLORS),$| = 1)[0];

--bwana147