I have an old and very large Perl Tk GUI application that I'm refactoring to Tkx. I want to split the interface into several packages so I can build up the application UI in a modular manner. Also, I want to keep the View separate from the Model, using a Controller to provide an interface between the two.
It seems to me that the only way to design this is with two huge global variables, one holding the model ($MODEL), and the other holding references to the widgets ($UI) that are spread across many packages. Then, the Controller interfaces the two using a series of commands like the following:
$UI->{'entry_widget'}->configure(-variable=>\$MODEL->{'entry_value'});
$UI->{'button_widget'}->configure(-command=>sub {$MODEL->{'entry_value'} = "New Value"} );
My question is: Is there a better way to design the application which avoids having to use these two big globals ($UI and $MODEL)? Any suggestions would be very welcome.
You're not looking to avoid globals, you're looking to use methods, that is replace
$hashref->{data}
with$model->data
or$self->model->data
, where$model
or$self
, is an argument (or a "singleton" as Axeman demonstrates) passed to a signal-handler/callback/command, not a hash you access directlyYou use methods to modify the
$model
, because methods can refuse to update the model with nonsense/incorrect data, they make sure you're not trying to pay with monopoly-moneyYour app will always create a model variable, and a view variable, and connect them (maybe via an intermediary, a controller) through argument passing
They don't have to be actual global variables in the perl sense ( Coping with Scoping ), they can be
my $variables
and still work fine just the way you're using them now (via closures), and you avoid the problems of http://perl.plover.com/varvarname.html, but you don't get the benefits of smart models that know what kind of fuel they need (diesel or unleaded); and connecting your views to your model is more typingSee also the answers and links from What is Model View Presenter?