Last active
February 24, 2017 03:15
-
-
Save jeffa/107b2de63ace11a17f4a to your computer and use it in GitHub Desktop.
Minimal example of an MVC implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl -l | |
| package Model; | |
| use Moose; | |
| has state => ( is => 'ro', reader => 'get_state', default => 0 ); | |
| sub set_state { $_[0]->{state} += $_[1] } | |
| package View; | |
| use Moose; | |
| sub update_display { print $_[1]->get_state } | |
| package Controller; | |
| use Moose; | |
| has model => ( is => 'ro', default => sub { Model->new } ); | |
| has view => ( is => 'ro', default => sub { View->new } ); | |
| sub run { | |
| my ($model, $view) = ($_[0]->model, $_[0]->view); | |
| for (1, 5, 8, -20, 5, 2) { | |
| $model->set_state( $_ ); | |
| $view->update_display( $model ); | |
| } | |
| } | |
| package main; | |
| Controller->new->run; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have since learned that the View should start the engine, not the Controller. Live and learn. 🍕