- Perl ›
- Object Oriented ›
- Name collision prevention
#
Object - oriented advantage 1 Prevent name collisions
Modules written in object orientation are often easy to use . Writing object-oriented is just because it's convenient, not because "functional descriptions are old".
For the time being, I'll list the advantages of object-oriented modules that functional modules don't have.
1. Prevents name collisions
The first object-oriented advantage is that it prevents name collisions . Let's compare Encode, a functional module, with XML::Simple, an object-oriented module.
Encode
When you use Encode, you can use a function called decode.
use Encode; my $string = 'ah'; my $dec_string = decode('utf8', $string);
XML::Simple
Using XML::Simple doesn't allow me to use any function. Methods can be used from objects created by XML::Simple->new.
use XML::Simple; my $file = 'a.xml'; my $parser = XML::Simple->new; $parser->XMLin($file);
When I use Encode, I can use a function called decode, but what if a function called decode has already been defined somewhere?
This will cause a name collision . On the other hand, the object-oriented module XML::Parser does not import any functions. Object-oriented modules are users don't have to worry about name collisions .
This is the first advantage. The disadvantage of object-oriented modules is that they are verbose. Encode has a shorter description, isn't it?
As a guideline, it will be used frequently in the program, and if you do not specify complicated options, create it as a functional module to simplify the description.
On the other hand, for special purposes and complicated option specifications, object-oriented modules are easy to use without name conflicts.
In most cases it's better to create an object-oriented module. Think carefully before making a functional module.