http://qs321.pair.com?node_id=11145374


in reply to MVC and Tk

G'day Rolf,

Here's a short example.

#!/usr/bin/env perl use strict; use warnings; package Model; my $num; sub num { my ($new_num) = @_; $num = $new_num if defined $new_num; $num = 0 unless defined $num; return $num; } package View; use Tk; sub init { my ($num) = @_; my $mw = MainWindow::->new(-title => 'Tk MVC Example'); $mw->geometry('280x80+50+80'); $mw->Label(-textvariable => \$num)->pack(); $mw->Button( -text => 'Increment', -command => sub { ++$num } )->pack(); $mw->Button( -text => 'Exit', -command => sub { exit; } )->pack(); MainLoop; } package Controller; sub run { my ($start) = @_; View::init(Model::num($start)); } package main; Controller::run(@ARGV ? $ARGV[0] : undef);

In production, those packages would be in separate files, so the separation of M, V & C would be more obvious:

# Model.pm package Model; ... 1; # View.pm package View; ... 1; # Controller.pm package Controller; use Model; use View; ... 1; # pm_11145356_tk_mvc.pl ... use Controller; ...

As an interesting artefact of a "short example" with no validation, you can call 'pm_11145356_tk_mvc.pl fred' and see increments: fred, free, fref, etc.

— Ken