📜 ⬆️ ⬇️

Botox - we disperse moose and mice

There is a lot of magic in the Perl world. It fascinates and prepares a trap for anyone who starts using magic for any reason.
I would like to warn you against this, especially if you are not using a stupid trick, but true magic.
My advice is simple - choose a tool for the task, correctly assessing it.
For example, do not use Moose or Mouse, if you just reluctance to write every time
my $self = bless({}, ref $invocant || $invocant); .
You hammer nails with a microscope.
Perl is phenomenally convenient with respect to OO-style programming, the whole point is that this is not obvious.
All that is required to achieve samathi is to thoughtfully re-read the chapter on typeglob a couple of times.
Or use the package Botox .

The package is simple to indecency. I just inject your class properties into its own (class) namespace, wrapping a candy in a closure. Voila!
Yes, this is not magic - it's just a clever trick, but what's the difference? It really works, and works well ( author's note - I hope that is right in both points ).

The class is created like this:

{{{package Parent;
use Botox qw(new prepare set_multi AUTOLOAD); # now you KNOW how mach is it cost.
my $object = new Parent(qw(name adress_rw surname_ro));
1;
}}}


An object instance is created like this:
')
{{{package Child;
my $foo = new Parent;
$foo->set_multi(name=>'Dolly',adress=>'Scotland, Newerland');
}}}


Botox not only eliminates the need to duplicate class creation code, but as you can see, it adds a couple of extremely useful things:

1. since our properties actually become methods (strange, but still) - they are inherited. Actually, it is worth reminding you that in the inheritance tree, the left upper parent takes precedence over the lower left, and the left lower one - above the one to the right. Families are especially close even in the seventh knee above distant relatives.

2. since our properties are so strange, no one will forbid us to make them even more strange - for example, to give them the opportunity to have the write permission flag from the object instance. Sometimes it is very useful to have a property available directly for reading and indirectly written (via an accessor or as a result of some more cunning manipulations in the class code). Simply add the suffix ' _ro ' to the name of the property in order to close it from a direct change from the object instance. The suffix ' _rw ' allows change from anywhere and is the default. You are free to write or not to write it, and I do not intend to indicate to you which is more correct. If by some strange coincidence, my suffixes intersect with your favorite way of designating class properties - let me know. It will be interesting to read WHAT has inspired you to such a syntax :)

3. No one will be against the possibility of initializing the values ​​of an instance (a reminder, rw-only) with chocks, in order not to write a string of calls. Easy! Use the set_multi method. True, if you get paid for SLOC - remove the method from the prototype. And in general - write in assembler!

I would be glad if Botox will be useful to you, well, or if you write something more elegant.

Source: https://habr.com/ru/post/76164/


All Articles