in reply to selfloader and anonymous subroutines
Why don't you just break out each sub into a separate file. Then, you just
&main_screen if $screen eq 'main';[update: argh, I just read your requirements more closely. Still I think you can slot it right in. As soon as you call &{$screen{$screen}} you'll hit the AUTOLOAD which will proceed as before but instead you'll want to do the file, and assign the results to $screen{$screen}} before executing the sub]
Then, all you would have to do is to define your AUTOLOAD to requires the desired file, which could be named something like main.screen (long live self-documenting file names), which defines main_screen... and then run that.
This would bring in the code on demand. If you changed the code in main.screen however, you would have to stop the program and restart it. If this is a problem, you could then start to get fancy, using anonymous subs, and reload the code on the fly if you notice that the file modification time has changed. I have written a Net::IRC bot that does this kind of trickery. I can add new code functionality to the bot whilst it is connected to a channel. The necessary smarts to handle keeping track of files is abstracted into a package.
|
|---|