Perl bless Function

Advertisements


Description

This function tells the entity referenced by REF that it is now an object in the CLASSNAME package, or the current package if CLASSNAME is omitted. Use of the two-argument form of bless is recommended.

Syntax

Following is the simple syntax for this function:

bless REF, CLASSNAME

bless REF

Return Value

This function returns the reference to an object blessed into CLASSNAME.

Example

Following is the example code showing its basic usage, the object reference is created by blessing a reference to the package's class.:

#!/usr/bin/perl

package Person;
sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
    # Print all the values just for clarification.
    print "First Name is $self->{_firstName}\n";
    print "Last Name is $self->{_lastName}\n";
    print "SSN is $self->{_ssn}\n";
    bless $self, $class;
    return $self;
}

When above code is executed, it produced following result




Advertisements
Advertisements