1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#!/usr/bin/perl use warnings; use strict; package Model { sub new { my $classname = shift; my $self = { controller => shift}; return bless($self, $classname); } sub calculateThreePlusFour { my $self = shift; return 3 + 4; } } package View { sub new { my $classname = shift; my $self = { controller => shift}; return bless($self, $classname); } sub showResult { my $self = shift; my $result = shift; print "$result\n"; } } package Controller { sub new { my $classname = shift; my $self = {}; $self->{model} = Model->new($self); $self->{view} = View->new($self); return bless($self, $classname); } sub run { my $self = shift; my $result = $self->{model}->calculateThreePlusFour(); $self->{view}->showResult($result); } } my $controller = Controller->new(); $controller->run();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package View { sub new { my $classname = shift; my $self = { controller => shift}; $self->{mw} = MainWindow->new(); $self->{mw}->optionAdd("*font", "Arial 18 normal"); $self->{mw}->title("Result"); $self->{tw} = $self->{mw}->Text(bg => "white", width => 20, height => 5); $self->{tw}->pack(-padx => 20, -pady => 20); $self->{btn_exit} = $self->{mw}->Button(-text => "Exit", -command => sub { $self->{mw}->destroy() }); $self->{btn_exit}->pack(-side => "right", -padx => 5, -pady => 5); return bless($self, $classname); } sub showResult { my $self = shift; my $result = shift; $self->{tw}->insert("end", $result); $self->{mw}->MainLoop(); } }