Class::Rebless - Rebase namespaces, hierarchically
use Class::Rebless; my $beat = bless({ one => bless({ hey => 'ho', }, 'AOne'), two => bless({ list => [ bless({ three => 3 }, 'AThree'), bless({ four => 4 }, 'AFour'), 5, "this is just noise", ], }, 'ATwo'), six => { seven => bless({ __VALUE__ => 7}, 'ASeven'), eight => bless({ __VALUE__ => 8}, 'AnEight'), }, }, 'AOne'); Class::Rebless->rebase($beat, 'And'); # $beat now contains objects of type # And::AOne, And::ATwo .. And::AnEight! Class::Rebless->rebless($beat, 'Beatless'); # All (blessed) objects in $beat now belong to package # Beatless.
Class::Rebless takes a Perl data structure and recurses through its hierarchy, reblessing objects that it finds along the way into new namespaces. This is typically useful when your object belongs to a package that is too close to the main namespace for your tastes, and you want to rebless everything down to your project's base namespace.
Class::Rebless walks scalar, array, and hash references. It uses Scalar::Util::reftype to discover how to walk blessed objects of any type.
Class::Rebless defines only class methods. There is no instance constructor, and when calling these methods you should take care not to call them in function form by mistake; that would not do at all.
Class::Rebless->rebless($myobj, "New::Namespace");
Finds all blessed objects refered to by $myobj and reblesses them into New::Namespace. This completely overrides whatever blessing they had before.
Class::Rebless->rebase($myobj, "New::Namespace::Root");
Finds all blessed objects refered to by $myobj and reblesses them into new namespaces relative to New::Namespace::Root. This overrides whatever blessing they had before, but unlike rebless, it preseves something of the original name. So if you had an object blessed into "MyClass", it will now be blessed into "New::Namespace::Root::MyClass".
Class::Rebless->custom($myobj, "MyName", { editor => \&my_editor });
Per each visited object referenced in $myobj, calls my_editor() on it. The editor routine is passed the current object in the recursion and the wanted namespace ("MyName" in the code above). This lets you to do anything you like with each object, but is (at least nominally) intended to allow filtering out objects you don't want to rebless. 3rd party objetcs, for example:
my $fh = IO::File->new("data") or die "open:$!"; my $frobber = Frotz->new({ source => $fh }); Class::Rebless->custom($frobber, "SuperFrotz", { editor => \&noio }); sub noio { my($obj, $namespace) = @_; return if ref($obj) =~ /^IO::/; bless $obj, $namespace . '::' . ref $obj; }
(A more realistic example might actually use an inclusion filter, not an inclusion filter.)
Reblessing a tied object may produce unexpected results.
Add a "prune" feature, most likely by specifying a magic return value for custom rebless editors.
Write a proper test suite (currently a rudimentary unit test is available by running "perl Class/Rebless.pm")
Gaal Yahas <gaal@forum2.org>
Copyright (c) 2004 Gaal Yahas. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Class::Rebless, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Rebless
CPAN shell
perl -MCPAN -e shell install Class::Rebless
For more information on module installation, please visit the detailed CPAN module installation guide.