The idea is this: if, say, you use a certain environment, some classes and modules, you may want to use any class-conductor between the usual classes and directly by you.
Here you can even come up with a few positive points in this. For example, you do not connect all the classes in each script, creating in the code header a slender column from the usual “use”. Well, like, save space. You can also think of it this way: you are not using a direct connection to a class and calling its methods through a class explorer, which does not prevent you from replacing one class with another and not even noticing the difference. Of course, if the methods match in both classes :-)
One way or another, you should have something like this:
my $name = $myClass->cgi->param( 'name' );
my $date = $myClass->date->get_local_date();
That is, your class explorer refers to a pseudonym, an internal representation of the desired class. Something like that, in general ...
')
To do this, in our class-conductor, we will organize a hash table of representations of the necessary classes:
my $classes = { cgi => CGI, date => My::Date::Module };
In fact, the first link in the $ myClass-> cgi-> param chain is the method. A method that is not and will not be in our class conductor, for the simple reason that it is generally surprising to write a pile of methods for using classes. We will use what the Perl AUTOLOAD method gives us, which is called if the class method is not found. Accordingly, the method itself is in the $ AUTOLOAD variable:
sub AUTOLOAD {
my $self = shift;
$self->{$sub} = $classes->{$sub}->new unless $self->{$sub};
return $self->{$sub};
}
This method only creates an instance of the class if it is not created or simply returns it back if it already exists.
This method has several disadvantages. For example, it is not clear to the end why he is needed =) Just kidding. It may come in handy. But not in those cases where the class you need in the designer contains a lot of useful settings that you often change. And the rest - cool.