Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

bless

Associates a reference with a package, turning it into an object.

Marks the referent so that method calls will dispatch through the given class (or the current package if no class is specified). The reference is modified in place and also returned, enabling the common return bless {}, $class pattern.

Handles multiple referent types: for Av and Hv containers the blessing is stored via interior mutability on the container itself. For Rv wrapping an Av or Hv, the inner container is blessed. For scalar references, a new blessed RvInner is created.

Synopsis

my $obj = bless {}, 'MyClass';
my $obj = bless {};              # blesses into current package
my $obj = bless \$scalar, 'Foo';
my $obj = bless [], 'Bar';

See Also

ref, Scalar::Util