HelloWorld
From WxPerl wiki
This page shows Hello.pl, a simple, bare, not-very-extensible, yet functional WxPerl program. It also shows a more robust example.
Contents |
Hello.pl
#!perl
use Wx;
# First, define an application object class to encapsulate the application itself
package HelloWorld;
use base 'Wx::App';
# We must override OnInit to build the window
sub OnInit
{
my $self = shift;
my $frame = Wx::Frame->new(undef, # no parent window
-1, # no window id
'Hello, World!', # Window title
[-1, -1], # Position
[-1, -1], # Size
);
my $panel = Wx::Panel->new($frame);
my $label = Wx::StaticText->new($panel, # Parent window
-1, # no window id
'Welcome to the world of WxPerl!',
[20, 20], # Position
);
$frame->Show(1);
return 1;
}
# Create the application object, and pass control to it.
package main;
my $app = HelloWorld->new;
$app->MainLoop;
Notes
This is not an example of good WxPerl programming style. It is simply an example of the minimal elements that need to be present in a WxPerl program. It is also useful for testing that your installation is set up properly.
In a real program, your top-level window would not be a bare Wx::Frame object; you would create your own frame class that would inherit from Wx::Frame. It would then draw itself, creating its own controls and positioning them, freeing the application object from having to worry about those details.
A Better Hello
In your project's library directory, file MyApp.pm:
=head1 NAME
MyApp - The controlling application object for My Project.
=cut
use Modern::Perl; # Optional but recommended
package MyApp;
use Wx;
use base 'Wx::App'; # This inherits from Wx::App
use MyFrame;
# Here, we'll store object instance data in a class hash
my %frame;
# We must provide an OnInit method which sets up the Application object
sub OnInit
{
my $self = shift;
# Create, store, and display the main window:
$frame{$self} = MyFrame->new;
$frame{$self}->Show;
return 1; # true value indicates success
}
# Clean up object data
sub DESTROY
{
my $self = shift;
delete $frame{$self};
}
1; # Perl modules must return a true value.
In your project's library directory, file MyFrame.pm:
=head1 NAME
MyFrame - The main window for My Project.
=cut
use Modern::Perl; # Optional but recommended
package MyFrame;
use Wx;
use base 'Wx::Frame';
# Constructor
sub new
{
my $class = shift;
my $title = @_? shift : 'Hello World!';
# Invoke Wx::Frame's construtor to build the frame object
my $self = $class->SUPER::new ( undef, # top-level window
-1, # no window ID
$title,
[-1, -1], # Allow the system to position the window
[-1, -1], # Default window size
);
# Create a panel to hold the window's controls
my $panel = Wx::Panel->new($self);
Wx::StaticText->new($panel, # Parent window
-1, # no window ID
'Welcome to the world of WxPerl!',
[20, 20],
);
return $self;
}
1; # Perl modules must return a true value
In your project's application directory, file main.pl:
#!perl use Modern::Perl; # Optional but recommended use lib 'lib'; # Point to this project's libraries use MyApp; my $app = MyApp->new; $app->MainLoop;
Notes
This is a lot more code, but it is a lot more organized, and this organization is essential to larger projects.
Perl veterans will recognize the "inside-out" object construction in MyApp.pm.
Moving to the Real World
In a more real-world program, many of the frame's controls would in turn be subclassed custom classes for the purpose of displaying specialized data, so the frame would not have to worry about the details. The frame's constructor would probably accept more arguments, and have to parse and validate them. The frame object would likely have its own data attributes.
The application object might pass itself as an argument to the frame. That way, the frame can ask the application for the data it needs to display -- the application object takes care of managing the global data for the program, and the frame object takes care of displaying the data and handling events from the user (and probably reporting those events back to the application object).
