hi
i am not getting what exactly bless function do in perl explanation in perldoc is not very clear i tried to search on google but i am getting confused or rather not getting at all. can anybody explain in short what it does in following example as well as in general ?
sub new {
my $class = shift;
my $self = {};
bless ($self, $class);
}
The bless() function turns a vanilla reference (that can be virtually any type of reference, although hash reference is used most of the time) into an object. Without bless(), you are not doing object-oriented programming in Perl.
The first line captures the package name. If one calls
my $obj = Some::Package->new();
(1) The package name "Some::Package" will be passed as the first argument if the arrow -> is used. I forgot exactly which manpage mentioned this, but it should be there.
(2) You just create a reference type that holds the model of the object. However, the object itself has not been created yet.
(3) This is what creates the object. The reference (model) is associated with the given package name, so making the resulting object an instance of that package. (In OOP, objects are instances of a package/class/module).
It looks bizarre, but that's the way Perl objects work.