How can I unbless an object in Perl?

Eugene Yarmash picture Eugene Yarmash · Feb 24, 2010 · Viewed 17.5k times · Source

From perldoc -f bless:

bless REF,CLASSNAME

This function tells the thingy referenced by REF that it is now
an object in the CLASSNAME package.

Is there any way of obtaining an unblessed structure without unnecessary copying?

Answer

Sinan Ünür picture Sinan Ünür · Feb 24, 2010

Data::Structure::Util

unbless($ref)

Remove the blessing from any objects found within the passed data structure.

#!/usr/bin/perl

use strict; use warnings;

use Scalar::Util qw( refaddr );
use Data::Structure::Util qw( unbless );

my $x = bless { a => 1, b => 2 } => 'My';

printf "%s : %s\n", ref $x, refaddr $x;

unbless $x;

printf "%s : %s\n", ref $x, refaddr $x;

Output:

My : 237356
HASH : 237356