in reply to caching formats

Wow ... a blast from the past.   (Lessee ... ADD 1 TO PERL GIVING PERL. ...)

As far as I am aware, formats are not “variables,” so you can’t “store” them anywhere.   You always refer to them by name, through $~ ($FORMAT_NAME) and $^ ($FORMAT_TOP_NAME).   This will get you where you want to go here.

To associate a particular format with a particular output channel, you first select() that channel (see perlfunc), to make it be the “default,” then you assign those variables (see perlvar), which will affect the channel which you just selected.   (Each channel has its own associated format-names, which default to the stream name.)   These two variables always describe the formats for the stream that is currently select()ed.

If your program first created all of the formats that it needed, assigning each one of them a unique format-name, then it could switch between them simply by-name, using any sort of data-structure to keep track of the names that had been generated.   That’s what you cache:   the (unique ...) names, which should be generated once.

For instance, let’s say you are using a hash which is (somehow) keyed by “things that you need to have formats for.”   The value for entries in this hash will be format-names.   If exists() tells you that a hash-key already exists, then the value associated with that key is the format-name that you should now use.   If a hash-key does not yet exist, then use eval() to coin a new one ... giving it a new, unique but arbitrary (say, sequential) format-name.   Store this name in the hash, so that the next time you need to format that same thing, you won’t create it again.   In this way, formats are eval’s on-demand, the first time they are needed.   (And of course, if you need to format several things the same way, simply refer to the same name.)