0

As many Perl developers know, unbless is used to remove the blessing from objects.
I am trying to understand how does it internally work by investigating its implementation.
I tried to check its implementation in its package Data::Structure::Util. Here's the Source:

sub unbless {
    unbless_xs( $_[0] );
}

Where may I find unbless_xs sub implementation/source ?

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
  • 2
    I've been a Perl developer for twenty years and have never heard of `unbless`... – Sean Aug 15 '15 at 20:12
  • Changed 'any' to 'many' :) ... So how do you unbless objects ? – Ashraf Bashir Aug 15 '15 at 20:13
  • 4
    ***"As any Perl developer knows, unbless is used to remove the blessing from objects"*** No. `unbless` is a function exported by `Data::Structure::Util`. It isn't a core module and I have never seen it used. If you need to `unbless` an object then your design is dreadful. What is it yuou're trying to do? – Borodin Aug 15 '15 at 20:15
  • @Borodin, my final destination is to have a JSON object as a return from some webservices, I have objects, and I need to unbless them as hashes then convert them to JSON objects. But I was just wondering from where unbless_xs came in the implementation of the module ? – Ashraf Bashir Aug 15 '15 at 20:20
  • quote: "If you need to unbless an object then your design is dreadful. " .... wondering why ? – Ashraf Bashir Aug 15 '15 at 20:21
  • @Borodin another question is opened to discuss your quote in details: http://stackoverflow.com/questions/32029042/is-unblessing-perl-objects-a-dreadful-design – Ashraf Bashir Aug 15 '15 at 20:28
  • 2
    Write a role for these objects that has a `TO_JSON` method. Even if you don't have control over how the objects are originally created, better to rebless them into a subclass that provides the additional functionality than to unbless them and start leaking the guts out all over the place. – DavidO Aug 16 '15 at 00:19
  • 1
    One reason that unblessing an object is poor design is because it intentionally breaks the encapsulation that the object provides, and exposes the reliant code on implementation details of the object. By, instead, subclassing the object and providing a TO_JSON method, you get to keep the encapsulation in place, and simply add the behavior your external code needs. It keeps internals where they should be, and keeps the external code free from reliance on specific object internal characteristics. If you cannot new the object as a subclass, write your subclass with the ability to rebless. – DavidO Aug 16 '15 at 00:25

2 Answers2

3

If you look for Data::Structure::Util on metacpan.org you will see a Browse link to the left that will let you explore the contents of the distribution

The same applies to any CPAN module: there is no need for a download to examine the source code. In this case you will find Util.xs at the root of the structure

If you prefer, you can do the same thing on the CPAN site search.cpan.org but you must ascend to the (currently) Data-Structure-Util-0.16 distribution to find the browse link

Borodin
  • 126,100
  • 9
  • 70
  • 144
2

It's implemented in C, you can check the source of the function at https://metacpan.org/source/ANDYA/Data-Structure-Util-0.16/Util.xs#L239

OneOfOne
  • 95,033
  • 20
  • 184
  • 185
  • Yup, I was searching for this, thanks a lot. I'll mark it as answer in 10 minutes – Ashraf Bashir Aug 15 '15 at 20:17
  • That's the source of `_unbless`. `_unbless_xs` is generated from the directives at line 858. `_unbless_xs` does call `_unbless`, though (`_unbless(sv, (HV*) sv_2mortal((SV*) newHV()));`) – ikegami Aug 16 '15 at 03:29