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.


--
g r i n d e r

In reply to Re: selfloader and anonymous subroutines by grinder
in thread selfloader and anonymous subroutines by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.