Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unbless an object in Perl?

Tags:

oop

perl

bless

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?

like image 725
Eugene Yarmash Avatar asked Feb 24 '10 20:02

Eugene Yarmash


1 Answers

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
like image 50
Sinan Ünür Avatar answered Oct 07 '22 12:10

Sinan Ünür