associated - A simple demo for one-to-one associations from UML (in Perl) (/examples).
1.00
Rumbaugh uses this simple example to illustrate a one-to-one association between a Country- and a City-object in ch. 3.2 of "Object Oriented Modelling".
The code may look clumsy and a little bit oversized for this simple problem. However the idea hopefully becomes evident: to access functions of other classes.
Associations are essential to come up with a compact and very powerful object-model (several distinct classes, with defined functionalities, with well established associations).
In this example the subroutine associate() assigns the Country- and the City-instances to link together country/city pairs. Running the debugger may produce results like this:
DB<6> x $ci1 0 City=HASH(0x81c6b3c) '_a_is_capital_from' => Country=HASH(0x81d04d0) '_a_has_capital' => City=HASH(0x81c6b3c) -> REUSED_ADDRESS '_name' => 'Germany' '_name' => 'Berlin' DB<7> x $ci1->get_name 0 'Berlin' DB<8> x $ci1->get_a_is_capital_from()->get_name() 0 'Germany'
which is all right.
The general syntax to access functions from another associated class is:
class1.association.function_of_class2
which translates into, e.g.:
$ci3->get_a_is_capital_from()->greetings();
to call the greetings() function of the Country-class from the specific City-instance $ci3.
perldoc City.pm perldoc Country.pm
Name: Michael Schlueter
email: mschlue@cpan.org
Copyright (c) 2000, Michael Schlueter. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
To install Class::Classgen::New, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Classgen::New
CPAN shell
perl -MCPAN -e shell install Class::Classgen::New
For more information on module installation, please visit the detailed CPAN module installation guide.