Gtk3 Window Using Moose Object Oriented Techniques
Below is a presentation of a template used to begin a new Gtk3 project using Moose Object Oriented Techniques. The idea is that the Moose attribute option titled lazy will delay the builder (or default value generation) until the accessor method is called for the first time. For sake of convenience as well as clarity the private method titled ooo (Order of Operations) calls the accessor methods of the Moose lazy attributes in order to provide a sane Gtk3 window construction procedure. Private members are denoted with a preceding underscore. This template also acts, conveniently, as a Perl 5 module.
use common::sense;
my $doc1;
package MyDocument {
use Moose;
use common::sense;
use feature 'say';
use feature 'state';
use feature 'switch';
use Gtk3 -init;
use namespace::autoclean;
has 'mw' => (
is => 'ro',
isa => 'Gtk3::Window',
default => sub { my $mw = Gtk3::Window->new ('toplevel') or die 'Regrettably, there was an incident with the width and height as it has not yet been defined as 2 whole integer values in between 240 and 8192'; return ($mw); },
);
has '_f1' => (
is => 'ro',
isa => 'Gtk3::Frame',
lazy => 1,
default => sub { my $f1 = Gtk3::Frame->new ('Primary'); $self->mw->add($f1); return($f1); },
);
# Order of Operations
sub _ooo {
my $i;
($_) = @_;
$_->_f1;
}
sub main {
my $i;
($_) = @_;
MainLoop;
}
__PACKAGE__->meta->make_immutable;
}
$doc1 = MyDocument->new;
$doc1->_ooo;
$doc1->main unless caller;
1;
This method uses Moose object oriented techniques to provide a Gtk3 window as both a script as well as a module.
Written by Jason McVeigh
Related protips
2 Responses
I ♥ what I do and I hope you do too!
I made this!