2. Some backgrounds Prima
Prima is written in a more or less object-oriented programming language style (OOP). I'll introduce only the most necessary OOP-topics
as classes, objects, properties, methods and inheritance and try to keep it simple. Four things to know:
First thing to know
A (Prima) class is a structure, that contains a set of properties (data or attributes) and methods (= subroutines, normally to manipulate
the properties of a class). A class is -in other words- a kind of datatype.
You could also say that a class is a blueprint for creating objects. An object is an instance of a Prima class that
has access to the class members (properties and methods). Prima objects can be created as follows:
$my_prima_object = Prima::MainWindow-> create( # instead of 'create' you can use 'new'
);
$my_prima_object-> set(
size => [300, 200], # set the MainWindow size
text => 'Hello', # set the MainWindow title
);
The object
$my_prima_object is a reference to the class MainWindow (accessed via
Prima::MainWindow).
The variable
$my_prima_object invokes (calls) the
'set' method, using the OOP arrow operator -> to assign values to the properties
'size' and
'text'.
However, there is another way creating Prima objects, which is recommended:
$my_prima_object = Prima::MainWindow-> create( # instead of 'create' you can use 'new'
size => [300, 200], # set the MainWindow size
text => 'Hello', # set the MainWindow title
);
It's not wrong to use the
'set' method, but add as much parameters into
create() or
new(), at the very least to avoid excessive resize of the window and its initial flickering on the screen.
Second thing to knowIf classes are connected to each other, then they share properties and methods.
Example: the class MainWindow is derived from the mother class Window and inherits properties and methods from the mother class Window (derivation or inheritance implies reuse of code).
It's a common practice that the derived class is more specialized or optimized. This means that the derived class always contains new or modified properties
and methods of the mother, grandmother etc. class.
Third thing to knowAn object, i.e. an instance of class, has access to properties and methods of the class the object belongs to and its mother, grandmother etc. classes.
Example: to set the title of the MainWindow object, we assign a value to the property
'text' which is part of the mother class Window
and -in turn- derived from the Window's mother class Widget, i.e. the grandmother of MainWindow. The comment on the last code example could be more precise:
$my_prima_object invokes the
derived 'set' method,
using the OOP arrow operator -> to assign values to the
derived properties
'size' and
'text'.
Last thing to knowhttps://metacpan.org/dist/Prima gives
all you need to know about classes, explained with examples ... but be warned, it's a bit overwhelming. Study the next item and the remaining chapters of Part 4.
2.1 A note on the Prima documentation
- On https://metacpan.org/dist/Prima/view/pod/Prima/Window.pod
you can read about Prima::Window.
- Description: 'Prima::Window is a descendant of Prima::Widget class.' This means: Prima::Window class inherits properties and
methods of the Prima::Widget class. https://metacpan.org/dist/Prima/view/pod/Prima/Widget.pod
provides further information. It appears under API, properties, that 'backColor' is a property of the Widget class and -we can now conclude- also of the Window class. Under API, methods we can read that 'show' is a method of Widget class and consequently of the Window class.
Properties and methods of the mother class are of course not repeated in the documentation of the derived class unless they are redefined.
- https://metacpan.org/pod/Prima::Buttons introduces a new concept: module.
- 'Prima::Buttons' is an example of a module
- Description: 'Prima::Buttons provides two separate sets of classes: the button widgets and the grouping widgets. The button widgets include push buttons, check-boxes and radio buttons. The grouping widgets are designed for usage as containers for the check-boxes and radio buttons, however, any widget can be inserted in a grouping widget.'
- Conclusion: a module is apparently a container that contains a number of closely related classes (1). This means: shared properties and methods.
Footnotes