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


In reply to Re: MVC and Tk by kcott
in thread MVC and Tk by LanX

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.