http://www.perlmonks.org?node_id=521252


in reply to Re: Implementing Model-View-Controller
in thread Implementing Model-View-Controller

... and to extend jZeds excellent bare-bones example, here is a slightly modified version that renders correct output even if you modify the data module. The controller and view modules still work, obviating the anticipated "hassle" posed in the original question.

Note that the aforementioned 'hassle' really has more to do with 'loose coupling' rather than the specifics of MVC. Note also that OOP purists will not like the modification introduced here, but this is good enough to get the point across.

#!/usr/bin/perl -w use strict; package Model; our $data = { fruits=>'apples',veggies=>'carrots' ,meat=>'bacon',grains=>'pita bread' }; sub get_food { $data->{$_[0]} } package View; my $template = "I like %s, especially %s!\n"; sub show_food { printf $template, @_ } package main; # Controller; for my $foodtype(sort keys %{$Model::data}) { my $food = Model::get_food($foodtype); View::show_food($foodtype,$food); } __END__

Another benefit of MVC not mentioned elsewhere in this thread, is that you can put your HTML designer to work on the view (and even edit it in Dreamweaver or whatever) without them having to know how to program. This also takes another kind 'loose coupling' to pull it off correctly, as well as the politics necessary to let each team member do what they do best.


=oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV