=head2 Associate an object to any perl variable
-This can be useful for passing user data through limited APIs.
+This technique can be useful for passing user data through limited APIs.
+It is similar to using inside-out objects, but without the drawback of having to implement a complex destructor.
{
package Magical::UserData;
my ($var) = @_;
my $data = &getdata($var, $wiz);
unless (defined $data) {
- &cast($var, $wiz);
- $data = &getdata($var, $wiz);
- die "Couldn't cast UserData magic onto the variable" unless defined $data;
+ $data = \(my $slot);
+ &cast($var, $wiz, $slot)
+ or die "Couldn't cast UserData magic onto the variable";
}
$$data;
}