Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: MVC and Tk

by kcott (Archbishop)
on Jul 08, 2022 at 20:43 UTC ( #11145374=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145374]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2022-10-04 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (19 votes). Check out past polls.

    Notices?