Server IP : 103.119.228.120 / Your IP : 18.116.85.204 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/local/ssl/local/ssl/local/share/man/man3/ |
Upload File : |
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Variable::Magic 3" .TH Variable::Magic 3 "2015-10-06" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Variable::Magic \- Associate user\-defined magic to variables from Perl. .SH "VERSION" .IX Header "VERSION" Version 0.59 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Variable::Magic qw<wizard cast VMG_OP_INFO_NAME>; \& \& { # A variable tracer \& my $wiz = wizard( \& set => sub { print "now set to ${$_[0]}!\en" }, \& free => sub { print "destroyed!\en" }, \& ); \& \& 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]\en" }, \& copy_key => 1, \& op_info => VMG_OP_INFO_NAME, \& ); \& \& my %h = (_default => 0, apple => 2); \& cast %h, $wiz, \*(Aq_default\*(Aq; \& print $h{banana}, "\en"; # "0" (there is no \*(Aqbanana\*(Aq key in %h) \& $h{pear} = 1; # "key pear stored in helem" \& } .Ve .SH "DESCRIPTION" .IX Header "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 \s-1XS.\s0 .PP 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 \f(CW$!\fR, \f(CW$(\fR or \f(CW$^W\fR, the \f(CW%ENV\fR and \f(CW%SIG\fR hashes, the \f(CW@ISA\fR array, \f(CW\*(C`vec()\*(C'\fR and \f(CW\*(C`substr()\*(C'\fR lvalues, threads::shared variables... They all share the same underlying C \s-1API,\s0 and this module gives you direct access to it. .PP Still, the magic made available by this module differs from tieing and overloading in several ways : .IP "\(bu" 4 Magic is not copied on assignment. .Sp You attach it to variables, not values (as for blessed references). .IP "\(bu" 4 Magic does not replace the original semantics. .Sp 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 \f(CW\*(C`tie\*(C'\fR, where you have to provide fallbacks methods for all actions by usually inheriting from the correct \f(CW\*(C`Tie::Std*\*(C'\fR class and overriding individual methods in your own class. .IP "\(bu" 4 Magic is multivalued. .Sp You can safely apply different kinds of magics to the same variable, and each of them will be invoked successively. .IP "\(bu" 4 Magic is type-agnostic. .Sp 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. .IP "\(bu" 4 Magic is invisible at Perl level. .Sp Magical and non-magical variables cannot be distinguished with \f(CW\*(C`ref\*(C'\fR, \f(CW\*(C`tied\*(C'\fR or another trick. .IP "\(bu" 4 Magic is notably faster. .Sp 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. .PP The operations that can be overloaded are : .IP "\(bu" 4 \&\fIget\fR .Sp This magic is invoked when the variable is evaluated. It is never called for arrays and hashes. .IP "\(bu" 4 \&\fIset\fR .Sp This magic is called each time the value of the variable changes. It is called for array subscripts and slices, but never for hashes. .IP "\(bu" 4 \&\fIlen\fR .Sp 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 (\f(CW\*(C`for\*(C'\fR, \f(CW\*(C`map\*(C'\fR or \f(CW\*(C`grep\*(C'\fR). The length is returned from the callback as an integer. .Sp Starting from perl 5.12, this magic is no longer called by the \f(CW\*(C`length\*(C'\fR keyword, and starting from perl 5.17.4 it is also no longer called for scalars in any situation, making this magic only meaningful on arrays. You can use the constants \*(L"\s-1VMG_COMPAT_SCALAR_LENGTH_NOLEN\*(R"\s0 and \*(L"\s-1VMG_COMPAT_SCALAR_NOLEN\*(R"\s0 to see if this magic is available for scalars or not. .IP "\(bu" 4 \&\fIclear\fR .Sp This magic is invoked when the variable is reset, such as when an array is emptied. Please note that this is different from undefining the variable, even though the magic is called when the clearing is a result of the undefine (e.g. for an array, but actually a bug prevent it to work before perl 5.9.5 \- see the history). .IP "\(bu" 4 \&\fIfree\fR .Sp This magic is called when a variable is destroyed as the result of going out of scope (but not when it is undefined). It behaves roughly like Perl object destructors (i.e. \f(CW\*(C`DESTROY\*(C'\fR methods), except that exceptions thrown from inside a \fIfree\fR callback will always be propagated to the surrounding code. .IP "\(bu" 4 \&\fIcopy\fR .Sp When applied to tied arrays and hashes, this magic fires when you try to access or change their elements. .Sp Starting from perl 5.17.0, it can also be applied to closure prototypes, in which case the magic will be called when the prototype is cloned. The \*(L"\s-1VMG_COMPAT_CODE_COPY_CLONE\*(R"\s0 constant is true when your perl support this feature. .IP "\(bu" 4 \&\fIdup\fR .Sp This magic is invoked when the variable is cloned across threads. It is currently not available. .IP "\(bu" 4 \&\fIlocal\fR .Sp When this magic is set on a variable, all subsequent localizations of the variable will trigger the callback. It is available on your perl if and only if \f(CW\*(C`MGf_LOCAL\*(C'\fR is true. .PP The following actions only apply to hashes and are available if and only if \*(L"\s-1VMG_UVAR\*(R"\s0 is true. They are referred to as \fIuvar\fR magics. .IP "\(bu" 4 \&\fIfetch\fR .Sp This magic is invoked each time an element is fetched from the hash. .IP "\(bu" 4 \&\fIstore\fR .Sp This one is called when an element is stored into the hash. .IP "\(bu" 4 \&\fIexists\fR .Sp This magic fires when a key is tested for existence in the hash. .IP "\(bu" 4 \&\fIdelete\fR .Sp This magic is triggered when a key is deleted in the hash, regardless of whether the key actually exists in it. .PP You can refer to the tests to have more insight of where the different magics are invoked. .SH "FUNCTIONS" .IX Header "FUNCTIONS" .ie n .SS """wizard""" .el .SS "\f(CWwizard\fP" .IX Subsection "wizard" .Vb 10 \& wizard( \& data => sub { ... }, \& get => sub { my ($ref, $data [, $op]) = @_; ... }, \& set => sub { my ($ref, $data [, $op]) = @_; ... }, \& len => sub { \& my ($ref, $data, $len [, $op]) = @_; ... ; return $newlen \& }, \& clear => sub { my ($ref, $data [, $op]) = @_; ... }, \& free => sub { my ($ref, $data [, $op]) = @_, ... }, \& copy => sub { my ($ref, $data, $key, $elt [, $op]) = @_; ... }, \& local => sub { my ($ref, $data [, $op]) = @_; ... }, \& fetch => sub { my ($ref, $data, $key [, $op]) = @_; ... }, \& store => sub { my ($ref, $data, $key [, $op]) = @_; ... }, \& exists => sub { my ($ref, $data, $key [, $op]) = @_; ... }, \& delete => sub { my ($ref, $data, $key [, $op]) = @_; ... }, \& copy_key => $bool, \& op_info => [ 0 | VMG_OP_INFO_NAME | VMG_OP_INFO_OBJECT ], \& ) .Ve .PP This function creates a 'wizard', an opaque object that holds the magic information. It takes a list of keys / values as argument, whose keys can be : .IP "\(bu" 4 \&\f(CW\*(C`data\*(C'\fR .Sp A code (or string) reference to a private data constructor. It is called in scalar context each time the magic is cast onto a variable, with \f(CW$_[0]\fR being a reference to this variable and \f(CW@_[1 .. @_\-1]\fR being all extra arguments that were passed to \*(L"cast\*(R". The scalar returned from this call is then attached to the variable and can be retrieved later with \*(L"getdata\*(R". .IP "\(bu" 4 \&\f(CW\*(C`get\*(C'\fR, \f(CW\*(C`set\*(C'\fR, \f(CW\*(C`len\*(C'\fR, \f(CW\*(C`clear\*(C'\fR, \f(CW\*(C`free\*(C'\fR, \f(CW\*(C`copy\*(C'\fR, \f(CW\*(C`local\*(C'\fR, \f(CW\*(C`fetch\*(C'\fR, \f(CW\*(C`store\*(C'\fR, \f(CW\*(C`exists\*(C'\fR and \f(CW\*(C`delete\*(C'\fR .Sp Code (or string) references to the respective magic callbacks. You don't have to specify all of them : the magic corresponding to undefined entries will simply not be hooked. .Sp When those callbacks are executed, \f(CW$_[0]\fR is a reference to the magic variable and \f(CW$_[1]\fR is the associated private data (or \f(CW\*(C`undef\*(C'\fR when no private data constructor is supplied with the wizard). Other arguments depend on which kind of magic is involved : .RS 4 .IP "\(bu" 8 \&\fIlen\fR .Sp \&\f(CW$_[2]\fR contains the natural, non-magical length of the variable (which can only be a scalar or an array as \fIlen\fR magic is only relevant for these types). The callback is expected to return the new scalar or array length to use, or \f(CW\*(C`undef\*(C'\fR to default to the normal length. .IP "\(bu" 8 \&\fIcopy\fR .Sp When the variable for which the magic is invoked is an array or an hash, \f(CW$_[2]\fR is a either an alias or a copy of the current key, and \f(CW$_[3]\fR is an alias to the current element (i.e. the value). Since \f(CW$_[2]\fR might be a copy, it is useless to try to change it or cast magic on it. .Sp Starting from perl 5.17.0, this magic can also be called for code references. In this case, \f(CW$_[2]\fR is always \f(CW\*(C`undef\*(C'\fR and \f(CW$_[3]\fR is a reference to the cloned anonymous subroutine. .IP "\(bu" 8 \&\fIfetch\fR, \fIstore\fR, \fIexists\fR and \fIdelete\fR .Sp \&\f(CW$_[2]\fR is an alias to the current key. Note that \f(CW$_[2]\fR may rightfully be readonly if the key comes from a bareword, and as such it is unsafe to assign to it. You can ask for a copy instead by passing \f(CW\*(C`copy_key => 1\*(C'\fR to \*(L"wizard\*(R" which, at the price of a small performance hit, allows you to safely assign to \f(CW$_[2]\fR in order to e.g. redirect the action to another key. .RE .RS 4 .Sp Finally, if \f(CW\*(C`op_info => $num\*(C'\fR is also passed to \f(CW\*(C`wizard\*(C'\fR, then one extra element is appended to \f(CW@_\fR. Its nature depends on the value of \f(CW$num\fR : .IP "\(bu" 8 \&\f(CW\*(C`VMG_OP_INFO_NAME\*(C'\fR .Sp \&\f(CW$_[\-1]\fR is the current op name. .IP "\(bu" 8 \&\f(CW\*(C`VMG_OP_INFO_OBJECT\*(C'\fR .Sp \&\f(CW$_[\-1]\fR is the \f(CW\*(C`B::OP\*(C'\fR object for the current op. .RE .RS 4 .Sp Both result in a small performance hit, but just getting the name is lighter than getting the op object. .Sp These callbacks are always executed in scalar context. The returned value is coerced into a signed integer, which is then passed straight to the perl magic \s-1API.\s0 However, note that perl currently only cares about the return value of the \fIlen\fR magic callback and ignores all the others. Starting with Variable::Magic 0.58, a reference returned from a non\-\fIlen\fR magic callback will not be destroyed immediately but will be allowed to survive until the end of the statement that triggered the magic. This lets you use this return value as a token for triggering a destructor after the original magic action takes place. You can see an example of this technique in the cookbook. .RE .PP Each callback can be specified as : .IP "\(bu" 4 a code reference, which will be called as a subroutine. .IP "\(bu" 4 a string reference, where the string denotes which subroutine is to be called when magic is triggered. If the subroutine name is not fully qualified, then the current package at the time the magic is invoked will be used instead. .IP "\(bu" 4 a reference to \f(CW\*(C`undef\*(C'\fR, in which case a no-op magic callback is installed instead of the default one. This may especially be helpful for \fIlocal\fR magic, where an empty callback prevents magic from being copied during localization. .PP Note that \fIfree\fR magic is never called during global destruction, as there is no way to ensure that the wizard object and the callback were not destroyed before the variable. .PP Here is a simple usage example : .PP .Vb 6 \& # A simple scalar tracer \& my $wiz = wizard( \& get => sub { print STDERR "got ${$_[0]}\en" }, \& set => sub { print STDERR "set to ${$_[0]}\en" }, \& free => sub { print STDERR "${$_[0]} was deleted\en" }, \& ); .Ve .ie n .SS """cast""" .el .SS "\f(CWcast\fP" .IX Subsection "cast" .Vb 1 \& cast [$@%&*]var, $wiz, @args .Ve .PP This function associates \f(CW$wiz\fR magic to the supplied variable, without overwriting any other kind of magic. It returns true on success or when \f(CW$wiz\fR magic is already attached, and croaks on error. When \f(CW$wiz\fR provides a data constructor, it is called just before magic is cast onto the variable, and it receives a reference to the target variable in \f(CW$_[0]\fR and the content of \f(CW@args\fR in \f(CW@_[1 .. @args]\fR. Otherwise, \f(CW@args\fR is ignored. .PP .Vb 3 \& # Casts $wiz onto $x, passing (\e$x, \*(Aq1\*(Aq) to the data constructor. \& my $x; \& cast $x, $wiz, 1; .Ve .PP The \f(CW\*(C`var\*(C'\fR argument can be an array or hash value. Magic for these scalars behaves like for any other, except that it is dispelled when the entry is deleted from the container. For example, if you want to call \f(CW\*(C`POSIX::tzset\*(C'\fR each time the \f(CW\*(AqTZ\*(Aq\fR environment variable is changed in \f(CW%ENV\fR, you can use : .PP .Vb 2 \& use POSIX; \& cast $ENV{TZ}, wizard set => sub { POSIX::tzset(); () }; .Ve .PP If you want to handle the possible deletion of the \f(CW\*(AqTZ\*(Aq\fR entry, you must also specify \fIstore\fR magic. .ie n .SS """getdata""" .el .SS "\f(CWgetdata\fP" .IX Subsection "getdata" .Vb 1 \& getdata [$@%&*]var, $wiz .Ve .PP This accessor fetches the private data associated with the magic \f(CW$wiz\fR in the variable. It croaks when \f(CW$wiz\fR does not represent a valid magic object, and returns an empty list if no such magic is attached to the variable or when the wizard has no data constructor. .PP .Vb 3 \& # Get the data attached to $wiz in $x, or undef if $wiz \& # did not attach any. \& my $data = getdata $x, $wiz; .Ve .ie n .SS """dispell""" .el .SS "\f(CWdispell\fP" .IX Subsection "dispell" .Vb 1 \& dispell [$@%&*]variable, $wiz .Ve .PP The exact opposite of \*(L"cast\*(R" : it dissociates \f(CW$wiz\fR magic from the variable. This function returns true on success, \f(CW0\fR when no magic represented by \f(CW$wiz\fR could be found in the variable, and croaks if the supplied wizard is invalid. .PP .Vb 2 \& # Dispell now. \& die \*(Aqno such magic in $x\*(Aq unless dispell $x, $wiz; .Ve .SH "CONSTANTS" .IX Header "CONSTANTS" .ie n .SS """MGf_COPY""" .el .SS "\f(CWMGf_COPY\fP" .IX Subsection "MGf_COPY" Evaluates to true if and only if the \fIcopy\fR magic is available. This is the case for perl 5.7.3 and greater, which is ensured by the requirements of this module. .ie n .SS """MGf_DUP""" .el .SS "\f(CWMGf_DUP\fP" .IX Subsection "MGf_DUP" Evaluates to true if and only if the \fIdup\fR magic is available. This is the case for perl 5.7.3 and greater, which is ensured by the requirements of this module. .ie n .SS """MGf_LOCAL""" .el .SS "\f(CWMGf_LOCAL\fP" .IX Subsection "MGf_LOCAL" Evaluates to true if and only if the \fIlocal\fR magic is available. This is the case for perl 5.9.3 and greater. .ie n .SS """VMG_UVAR""" .el .SS "\f(CWVMG_UVAR\fP" .IX Subsection "VMG_UVAR" When this constant is true, you can use the \fIfetch\fR, \fIstore\fR, \fIexists\fR and \fIdelete\fR magics on hashes. Initial \*(L"\s-1VMG_UVAR\*(R"\s0 capability was introduced in perl 5.9.5, with a fully functional implementation shipped with perl 5.10.0. .ie n .SS """VMG_COMPAT_SCALAR_LENGTH_NOLEN""" .el .SS "\f(CWVMG_COMPAT_SCALAR_LENGTH_NOLEN\fP" .IX Subsection "VMG_COMPAT_SCALAR_LENGTH_NOLEN" True for perls that don't call \fIlen\fR magic when taking the \f(CW\*(C`length\*(C'\fR of a magical scalar. .ie n .SS """VMG_COMPAT_SCALAR_NOLEN""" .el .SS "\f(CWVMG_COMPAT_SCALAR_NOLEN\fP" .IX Subsection "VMG_COMPAT_SCALAR_NOLEN" True for perls that don't call \fIlen\fR magic on scalars. Implies \*(L"\s-1VMG_COMPAT_SCALAR_LENGTH_NOLEN\*(R"\s0. .ie n .SS """VMG_COMPAT_ARRAY_PUSH_NOLEN""" .el .SS "\f(CWVMG_COMPAT_ARRAY_PUSH_NOLEN\fP" .IX Subsection "VMG_COMPAT_ARRAY_PUSH_NOLEN" True for perls that don't call \fIlen\fR magic when you push an element in a magical array. Starting from perl 5.11.0, this only refers to pushes in non-void context and hence is false. .ie n .SS """VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID""" .el .SS "\f(CWVMG_COMPAT_ARRAY_PUSH_NOLEN_VOID\fP" .IX Subsection "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID" True for perls that don't call \fIlen\fR magic when you push in void context an element in a magical array. .ie n .SS """VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID""" .el .SS "\f(CWVMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID\fP" .IX Subsection "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID" True for perls that don't call \fIlen\fR magic when you unshift in void context an element in a magical array. .ie n .SS """VMG_COMPAT_ARRAY_UNDEF_CLEAR""" .el .SS "\f(CWVMG_COMPAT_ARRAY_UNDEF_CLEAR\fP" .IX Subsection "VMG_COMPAT_ARRAY_UNDEF_CLEAR" True for perls that call \fIclear\fR magic when undefining magical arrays. .ie n .SS """VMG_COMPAT_HASH_DELETE_NOUVAR_VOID""" .el .SS "\f(CWVMG_COMPAT_HASH_DELETE_NOUVAR_VOID\fP" .IX Subsection "VMG_COMPAT_HASH_DELETE_NOUVAR_VOID" True for perls that don't call \fIdelete\fR magic when you delete an element from a hash in void context. .ie n .SS """VMG_COMPAT_CODE_COPY_CLONE""" .el .SS "\f(CWVMG_COMPAT_CODE_COPY_CLONE\fP" .IX Subsection "VMG_COMPAT_CODE_COPY_CLONE" True for perls that call \fIcopy\fR magic when a magical closure prototype is cloned. .ie n .SS """VMG_COMPAT_GLOB_GET""" .el .SS "\f(CWVMG_COMPAT_GLOB_GET\fP" .IX Subsection "VMG_COMPAT_GLOB_GET" True for perls that call \fIget\fR magic for operations on globs. .ie n .SS """VMG_PERL_PATCHLEVEL""" .el .SS "\f(CWVMG_PERL_PATCHLEVEL\fP" .IX Subsection "VMG_PERL_PATCHLEVEL" The perl patchlevel this module was built with, or \f(CW0\fR for non-debugging perls. .ie n .SS """VMG_THREADSAFE""" .el .SS "\f(CWVMG_THREADSAFE\fP" .IX Subsection "VMG_THREADSAFE" True if and only if this module could have been built with thread-safety features enabled. .ie n .SS """VMG_FORKSAFE""" .el .SS "\f(CWVMG_FORKSAFE\fP" .IX Subsection "VMG_FORKSAFE" True if and only if this module could have been built with fork-safety features enabled. This is always true except on Windows where it is false for perl 5.10.0 and below. .ie n .SS """VMG_OP_INFO_NAME""" .el .SS "\f(CWVMG_OP_INFO_NAME\fP" .IX Subsection "VMG_OP_INFO_NAME" Value to pass with \f(CW\*(C`op_info\*(C'\fR to get the current op name in the magic callbacks. .ie n .SS """VMG_OP_INFO_OBJECT""" .el .SS "\f(CWVMG_OP_INFO_OBJECT\fP" .IX Subsection "VMG_OP_INFO_OBJECT" Value to pass with \f(CW\*(C`op_info\*(C'\fR to get a \f(CW\*(C`B::OP\*(C'\fR object representing the current op in the magic callbacks. .SH "COOKBOOK" .IX Header "COOKBOOK" .SS "Associate an object to any perl variable" .IX Subsection "Associate an object to any perl variable" 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. .PP .Vb 2 \& { \& package Magical::UserData; \& \& use Variable::Magic qw<wizard cast getdata>; \& \& my $wiz = wizard data => sub { \e$_[1] }; \& \& sub ud (\e[$@%*&]) : lvalue { \& my ($var) = @_; \& my $data = &getdata($var, $wiz); \& unless (defined $data) { \& $data = \e(my $slot); \& &cast($var, $wiz, $slot) \& or die "Couldn\*(Aqt cast UserData magic onto the variable"; \& } \& $$data; \& } \& } \& \& { \& BEGIN { *ud = \e&Magical::UserData::ud } \& \& my $cb; \& $cb = sub { print \*(AqHello, \*(Aq, ud(&$cb), "!\en" }; \& \& ud(&$cb) = \*(Aqworld\*(Aq; \& $cb\->(); # Hello, world! \& } .Ve .SS "Recursively cast magic on datastructures" .IX Subsection "Recursively cast magic on datastructures" \&\f(CW\*(C`cast\*(C'\fR can be called from any magical callback, and in particular from \f(CW\*(C`data\*(C'\fR. This allows you to recursively cast magic on datastructures : .PP .Vb 10 \& my $wiz; \& $wiz = wizard data => sub { \& my ($var, $depth) = @_; \& $depth ||= 0; \& my $r = ref $var; \& if ($r eq \*(AqARRAY\*(Aq) { \& &cast((ref() ? $_ : \e$_), $wiz, $depth + 1) for @$var; \& } elsif ($r eq \*(AqHASH\*(Aq) { \& &cast((ref() ? $_ : \e$_), $wiz, $depth + 1) for values %$var; \& } \& return $depth; \& }, \& free => sub { \& my ($var, $depth) = @_; \& my $r = ref $var; \& print "free $r at depth $depth\en"; \& (); \& }; \& \& { \& my %h = ( \& a => [ 1, 2 ], \& b => { c => 3 } \& ); \& cast %h, $wiz; \& } .Ve .PP When \f(CW%h\fR goes out of scope, this prints something among the lines of : .PP .Vb 6 \& free HASH at depth 0 \& free HASH at depth 1 \& free SCALAR at depth 2 \& free ARRAY at depth 1 \& free SCALAR at depth 3 \& free SCALAR at depth 3 .Ve .PP Of course, this example does nothing with the values that are added after the \f(CW\*(C`cast\*(C'\fR. .SS "Delayed magic actions" .IX Subsection "Delayed magic actions" Starting with Variable::Magic 0.58, the return value of the magic callbacks can be used to delay the action until after the original action takes place : .PP .Vb 10 \& my $delayed; \& my $delayed_aux = wizard( \& data => sub { $_[1] }, \& free => sub { \& my ($target) = $_[1]; \& my $target_data = &getdata($target, $delayed); \& local $target_data\->{guard} = 1; \& if (ref $target eq \*(AqSCALAR\*(Aq) { \& my $orig = $$target; \& $$target = $target_data\->{mangler}\->($orig); \& } \& return; \& }, \& ); \& $delayed = wizard( \& data => sub { \& return +{ guard => 0, mangler => $_[1] }; \& }, \& set => sub { \& return if $_[1]\->{guard}; \& my $token; \& cast $token, $delayed_aux, $_[0]; \& return \e$token; \& }, \& ); \& my $x = 1; \& cast $x, $delayed => sub { $_[0] * 2 }; \& $x = 2; \& # $x is now 4 \& # But note that the delayed action only takes place at the end of the \& # current statement : \& my @y = ($x = 5, $x); \& # $x is now 10, but @y is (5, 5) .Ve .SH "PERL MAGIC HISTORY" .IX Header "PERL MAGIC HISTORY" The places where magic is invoked have changed a bit through perl history. Here is a little list of the most recent ones. .IP "\(bu" 4 \&\fB5.6.x\fR .Sp \&\fIp14416\fR : \fIcopy\fR and \fIdup\fR magic. .IP "\(bu" 4 \&\fB5.8.9\fR .Sp \&\fIp28160\fR : Integration of \fIp25854\fR (see below). .Sp \&\fIp32542\fR : Integration of \fIp31473\fR (see below). .IP "\(bu" 4 \&\fB5.9.3\fR .Sp \&\fIp25854\fR : \fIlen\fR magic is no longer called when pushing an element into a magic array. .Sp \&\fIp26569\fR : \fIlocal\fR magic. .IP "\(bu" 4 \&\fB5.9.5\fR .Sp \&\fIp31064\fR : Meaningful \fIuvar\fR magic. .Sp \&\fIp31473\fR : \fIclear\fR magic was not invoked when undefining an array. The bug is fixed as of this version. .IP "\(bu" 4 \&\fB5.10.0\fR .Sp Since \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR is uppercased, \f(CW\*(C`hv_magic_check()\*(C'\fR triggers \fIcopy\fR magic on hash stores for (non-tied) hashes that also have \fIuvar\fR magic. .IP "\(bu" 4 \&\fB5.11.x\fR .Sp \&\fIp32969\fR : \fIlen\fR magic is no longer invoked when calling \f(CW\*(C`length\*(C'\fR with a magical scalar. .Sp \&\fIp34908\fR : \fIlen\fR magic is no longer called when pushing / unshifting an element into a magical array in void context. The \f(CW\*(C`push\*(C'\fR part was already covered by \fIp25854\fR. .Sp \&\fIg9cdcb38b\fR : \fIlen\fR magic is called again when pushing into a magical array in non-void context. .SH "EXPORT" .IX Header "EXPORT" The functions \*(L"wizard\*(R", \*(L"cast\*(R", \*(L"getdata\*(R" and \*(L"dispell\*(R" are only exported on request. All of them are exported by the tags \f(CW\*(Aq:funcs\*(Aq\fR and \f(CW\*(Aq:all\*(Aq\fR. .PP All the constants are also only exported on request, either individually or by the tags \f(CW\*(Aq:consts\*(Aq\fR and \f(CW\*(Aq:all\*(Aq\fR. .SH "CAVEATS" .IX Header "CAVEATS" In order to hook hash operations with magic, you need at least perl 5.10.0 (see \*(L"\s-1VMG_UVAR\*(R"\s0). .PP If you want to store a magic object in the private data slot, you will not be able to recover the magic with \*(L"getdata\*(R", since magic is not copied by assignment. You can work around this gotcha by storing a reference to the magic object instead. .PP If you define a wizard with \fIfree\fR magic and cast it on itself, it results in a memory cycle, so this destructor will not be called when the wizard is freed. .SH "DEPENDENCIES" .IX Header "DEPENDENCIES" perl 5.8. .PP A C compiler. This module may happen to build with a \*(C+ compiler as well, but don't rely on it, as no guarantee is made in this regard. .PP Carp (core since perl 5), XSLoader (since 5.6.0). .SH "SEE ALSO" .IX Header "SEE ALSO" perlguts and perlapi for internal information about magic. .PP perltie and overload for other ways of enhancing objects. .SH "AUTHOR" .IX Header "AUTHOR" Vincent Pit, \f(CW\*(C`<perl at profvince.com>\*(C'\fR, <http://www.profvince.com>. .PP You can contact me by mail or on \f(CW\*(C`irc.perl.org\*(C'\fR (vincent). .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests to \f(CW\*(C`bug\-variable\-magic at rt.cpan.org\*(C'\fR, or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable\-Magic>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .SH "SUPPORT" .IX Header "SUPPORT" You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc Variable::Magic .Ve .SH "COPYRIGHT & LICENSE" .IX Header "COPYRIGHT & LICENSE" Copyright 2007,2008,2009,2010,2011,2012,2013,2014,2015 Vincent Pit, all rights reserved. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.