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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|