getdata#

Retrieve the private data this wizard’s attachment carries.

Synopsis#

use Peta::XS ':magic';
my $data = getdata(\$x, $w);   # whatever $w's data callback returned

What you get back#

The attachment’s private data - the value the wizard’s data callback returned at cast time - or undef when this wizard has no attachment on the variable or the wizard has no data callback. It is the same value the get/set/free callbacks receive as their second argument: if it is a reference, mutations made inside callbacks are visible here.

Examples#

## per-variable access statistics, read back after the fact

my $counted = wizard(
    data => sub { { reads => 0, writes => 0 } },
    get  => sub { $_[1]{reads}++ },
    set  => sub { $_[1]{writes}++ },
);
my $x = 1;
cast(\$x, $counted);
my $v = $x; $v = $x;         # two reads
$x = 2;                      # one write
my $stats = getdata(\$x, $counted);
say "$stats->{reads}/$stats->{writes}";   # 2/1

## distinguishing "no attachment" from "no data"

say defined getdata(\$x, $counted) ? 'attached' : 'not here';

See also#

  • cast - where the data callback runs.

  • wizard - declaring the data callback.