From: Vincent Pit Date: Tue, 23 Feb 2010 13:42:19 +0000 (+0100) Subject: Tune the userdata example X-Git-Tag: v0.41~3 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FVariable-Magic.git;a=commitdiff_plain;h=524bcc5a4d2ada4ea2a9ea1784e0f7d0b6acd0d9 Tune the userdata example --- diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm index 04ed9df..991bca1 100644 --- a/lib/Variable/Magic.pm +++ b/lib/Variable/Magic.pm @@ -417,7 +417,8 @@ Value to pass with C to get a C object representing the current =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; @@ -430,9 +431,9 @@ This can be useful for passing user data through limited APIs. 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; }