Perl classes, what about blessing hashes?

I don't understand why Perl's constructor needs all this blessing and why it is always done with a hash (perhaps apparently using other variable types).

How does it make sense that the constructor will return a blissful hash reference when I create a new instance of the class? I could understand "return (it)"; or something like that, but returning some other random variable just puzzles me (especially when you have to use a hash).

my ?var = new Test("foo");
package Test;
our $localvar;
sub new{
 localvar = $_[1];
}

      

OK, so I have this base class. I can set a class variable on initialization and then use it like $ var :: localvar. But for this to actually compile and work in Perl, I need to add the line "return bless {}, shift;"

It seems that this hash is being used as an instance of this class, with the methods being pretty static. But you can still have class variables. It looks like you are just binding a data object with a list of methods that take that object as an argument. But I'm not sure why every tutorial assumes that you always use a hash if that's all that's going on. And I'm confused why you have both this hash and any "ours" you stated in class, they seem to be mutually exclusive concepts?

+3


source to share


2 answers


It looks like you are just binding a data object with a list of methods that take that object as an argument.

What is OO, yes. The case you return from the constructor is an instance. Unlike some other languages, where the language creates a new "object instance" behind the scenes and your constructor is responsible for filling in the slots, in Perl your constructor method gets all the work. {}

creates a link to a new anonymous hash that will become the repository for the object, which bless

is what actually turns that link into an object by labeling it with its class name.

But I'm not sure why every tutorial assumes that you always use a hash if that's all that's going on.



A class can be any type of reference, but a hash reference is the most useful and convenient because hashes have named slots, so you can refer to your object properties by name. There are examples of globref objects (file and socket descriptors), arrayref objects (rare, usually used for multi-field objects when the author is very concerned about speed and memory usage), and scalarref objects (often used to encapsulate a pointer returned by some C library). But hashref objects are "standard".

I can set a class variable when I initialize it and then use it like var: localvar.

But why would you? Class variables are almost completely useless, and there is no reason to worry about them until you understand more basic and useful things.

+16


source


How does it make sense that the constructor will return a blissful hash reference when I create a new instance of the class?

Well, it would be a useless constructor if you didn't return the created object.

I could understand "return (this)"; or something like that

Then what is the confusion? This is exactly what you should be returning. (Except that this is a symbol $self

.)

sub new {
    my ($class, ...) = @_;
    my $self = bless({}, $class);  # Or: my $self = $class->SUPER::new(...);
    $self->{attribute1} = ...;
    $self->{attribute2} = ...;
    return $self;
}

      

It seems that this hash is being used as an instance of this class,

A blissful hash is an instance of this class that is an object.




Questions from comments

Why do I need to [do my $self = bless({}, $class);

] instead of just referring to the class instance. For example:$self = bless($class)

{}

allocates a variable and bless

binds it to the class. Two necessary steps to create an object.

By having two separate ones, you have the ability to use different base variables for your object. For example, IO :: Socket :: INET uses glob instead of hash.

But most of the objects that could do could not use a hash in my opinion. A hash is a fairly specific data structure that is unnecessary or unnecessary for 99% of the encoding.

It's not about using a hash table; it must use an associative array (where each element is an attribute).

Associative arrays (hash tables or others) are "necessary and useful" in much over 1% of the encoding.

+9


source







All Articles