bless 
blessREF,CLASSNAMEblessREF
This function tells the referent pointed to by reference
REF that it is now an object in the
CLASSNAME package—or the current package if
no CLASSNAME is specified. If
REF is not a valid reference, an exception is
raised. For convenience, bless
returns the reference, since it’s often the last function in a
constructor subroutine. For example:
$pet = Beast–>new(TYPE => "cougar", NAME => "Clyde");
# then in Beast.pm:
sub new {
my $class = shift;
my %attrs = @_;
my $self = { %attrs };
return bless($self, $class);
}You should generally bless objects
into CLASSNAMEs that are mixed case.
Namespaces with all lowercase
names are reserved for internal use as Perl pragmata (compiler directives). Built-in types (such as
“SCALAR”, “ARRAY”, “HASH”, “UNIVERSAL”, etc.) all have uppercase names, so you may wish to
avoid such package names as well.
Make sure that CLASSNAME is not false;
blessing into false packages is not supported and may result in
unpredictable behavior.
It is not a bug that there is no corresponding curse operator. (But there is a sin operator.) See also Chapter 12 for more about the blessing (and blessings) of
objects.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access