magic#

Inventory of the magic attached to the variable $ref points at.

Synopsis#

use Peta::XS ':facts';
tie my %h, 'Some::Tie';
for my $m (magic(\%h)) {
    printf "type=%s get=%d set=%d\n", $m->{type}, $m->{get}, $m->{set};
}

What you get back#

One hashref per attachment, in chain order (most recently attached first, mirroring perl’s prepend discipline): type is perl’s public one-character magic type (the perlguts vocabulary - ‘P’ tied array/hash, ‘q’ tied scalar, ‘~’ extension magic as used by cast), get/set are booleans saying whether the attachment hooks reads/writes. In scalar context: the number of attachments. An unmagical variable yields an empty list / 0.

Examples#

my $plain = 1;
say scalar magic(\$plain);          # 0

## is this hash really tied?

tie my %cfg, 'Config::Tied';
my ($m) = magic(\%cfg);
say "tied!" if $m && $m->{type} eq 'P';

## see your own wizards (and their hook shape) from the outside

use Peta::XS ':magic';
my $w = wizard(get => sub { });
my $x = 1;
cast(\$x, $w);
my @mg = magic(\$x);                # ({ type => '~', get => 1, set => 1 })

## debugging aid: why is every read of $slow slow?

printf "%s(get=%d,set=%d)\n", @{$_}{qw(type get set)}
    for magic(\$slow);

See also#