3 ms·
> What always kills me is the weirdness of the basic data structures, > array/stack function calls, and the truly impressively weird ways > Objects are done.
by G3rn0ti 3y ago
> What always kills me is the weirdness of the basic data structures,
> array/stack function calls, and the truly impressively weird ways
> Objects are done.
Well, Perl 5 has scalars, arrays and dictionaries -- just like Python. Unlike Python, however, Perl function arguments are always implicit/variadic by default (and stored inside the implicit "@_" variable). This can be a benefit or a curse depending on the context. Furthermore, Perl 5 does support both "call by value" and "call by reference". This distinction can confuse programmers coming from more modern languages where usually everything is a scalar reference. But you have to keep in mind Perl was invented in the 80s and put great emphasis on compatibility. Changing calling conventions would have broken many modules.
edit:
Regarding objects: The object system is rather simple, it's just many Perl hackers invented their own object systems on top of that. Basically, a Perl class is just a module. An object is just a reference to a dictionary that got the class name attached by applying the "bless" operator:
package Foo;
sub new {
my ($class) = @_;
my $object = {};
bless $object, $class;
}
sub do_stuff {
my ($self) = @_;
# do stuff
return $self;
}
my $obj = Foo->new();
$obj->do_stuff()->do_stuff();
That's about it! Well, you also have inheritance using "use base ..." and you can bless other fundamental types which usually is not a bright idea. Many other object oriented languages make object orientation unnecessarily much more complex (looking at you C++).
- Scubabear68 3y agoAll true. But you have to factor in all the historical baggage, things like the weirdness of Files, introduction of references,etc. It all just ends up feeling deeply weirds and unique compared to other languages. Hence the need to relearn it every time I touch it.
- thatwasunusual 3y agoOr my favorite: package Foo { use MooseX::Extended; sub do_stuff ($self) { # do stuff return $self; } } my $obj = Foo->new; $obj->do_stuff->do_stuff;