/usr/lib/x86_64-linux-gnu/perl5/5.34/Variable
Edit: /usr/lib/x86_64-linux-gnu/perl5/5.34/Variable/Magic.pm (23850B)
package Variable::Magic;
use 5.008;
use strict;
use warnings;
=head1 NAME
Variable::Magic - Associate user-defined magic to variables from Perl.
=head1 VERSION
Version 0.62
=cut
our $VERSION;
BEGIN {
$VERSION = '0.62';
}
=head1 SYNOPSIS
use Variable::Magic qw
;
{ # A variable tracer
my $wiz = wizard(
set => sub { print "now set to ${$_[0]}!\n" },
free => sub { print "destroyed!\n" },
);
my $a = 1;
cast $a, $wiz;
$a = 2; # "now set to 2!"
} # "destroyed!"
{ # A hash with a default value
my $wiz = wizard(
data => sub { $_[1] },
fetch => sub { $_[2] = $_[1] unless exists $_[0]->{$_[2]}; () },
store => sub { print "key $_[2] stored in $_[-1]\n" },
copy_key => 1,
op_info => VMG_OP_INFO_NAME,
);
my %h = (_default => 0, apple => 2);
cast %h, $wiz, '_default';
print $h{banana}, "\n"; # "0" (there is no 'banana' key in %h)
$h{pear} = 1; # "key pear stored in helem"
}
=head1 DESCRIPTION
Magic is Perl's way of enhancing variables.
This mechanism lets the user add extra data to any variable and hook syntactical operations (such as access, assignment or destruction) that can be applied to it.
With this module, you can add your own magic to any variable without having to write a single line of XS.
You'll realize that these magic variables look a lot like tied variables.
It is not surprising, as tied variables are implemented as a special kind of magic, just like any 'irregular' Perl variable : scalars like C<$!>, C<$(> or C<$^W>, the C<%ENV> and C<%SIG> hashes, the C<@ISA> array, C and C lvalues, L variables...
They all share the same underlying C API, and this module gives you direct access to it.
Still, the magic made available by this module differs from tieing and overloading in several ways :
=over 4
=item *
Magic is not copied on assignment.
You attach it to variables, not values (as for blessed references).
=item *
Magic does not replace the original semantics.
Magic callbacks usually get triggered before the original action takes place, and cannot prevent it from happening.
This also makes catching individual events easier than with C, where you have to provide fallbacks methods for all actions by usually inheriting from the correct C class and overriding individual methods in your own class.
=item *
Magic is multivalued.
You can safely apply different kinds of magics to the same variable, and each of them will be invoked successively.
=item *
Magic is type-agnostic.
The same magic can be applied on scalars, arrays, hashes, subs or globs.
But the same hook (see below for a list) may trigger differently depending on the type of the variable.
=item *
Magic is invisible at Perl level.
Magical and non-magical variables cannot be distinguished with C[, C or another trick.
=item *
Magic is notably faster.
Mainly because perl's way of handling magic is lighter by nature, and because there is no need for any method resolution.
Also, since you don't have to reimplement all the variable semantics, you only pay for what you actually use.
=back
The operations that can be overloaded are :
=over 4
=item *
I
This magic is invoked when the variable is evaluated.
It is never called for arrays and hashes.
=item *
I
This magic is called each time the value of the variable changes.
It is called for array subscripts and slices, but never for hashes.
=item *
I
This magic only applies to arrays (though it used to also apply to scalars), and is triggered when the 'size' or the 'length' of the variable has to be known by Perl.
This is typically the magic involved when an array is evaluated in scalar context, but also on array assignment and loops (C, C]