magic#
מצאי הקסם המצורף למשתנה ש־$ref מצביע עליו.
תקציר#
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};
}
מה מוחזר#
hashref אחד לכל צירוף, בסדר השרשרת (האחרון שצורף ראשון, בהתאם למשמעת ההוספה־בראש של perl): type הוא סוג הקסם הציבורי בן התו האחד של perl (אוצר המילים של perlguts - »P« מערך/האש קשור, »q« סקלר קשור, »~« קסם הרחבה כפי שמשמש את cast), get/set הם ערכים בוליאניים האומרים אם הצירוף מתחבר לקריאות/כתיבות. בהקשר סקלר: מספר הצירופים. משתנה ללא קסם מניב רשימה ריקה / 0.
דוגמאות#
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);
ראו גם#
refcount,value_slots- העובדות האחרות.perldoc perlguts- אוצר המילים של סוגי הקסם.