- Perl ›
- builtin functions ›
- here
bless function - create object
Use the bless function to create a object . In Perl, an object is the data associated with a class name. In most cases, hash reference are used for the data.
# Create object $obj = bless $data, $class;
This is an example constructor. I am creating an object by associating a class name with a hash reference.
# Constructor
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
This is an example of a general constructor. Argument processing, processing corresponding to when called from an object, etc. are added.
# General constructor
sub new {
my $proto = shift;
my $class = ref $proto || $proto;
my $self = ref $_[0] eq 'HASH' ? $_[0] : {@_};
return bless $self, $class;
}
Perl ABC