Variable::Magic

Associate user-defined magic to variables from Perl
Download

Variable::Magic Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Vincent Pit
  • Publisher web site:
  • http://search.cpan.org/~vpit/

Variable::Magic Tags


Variable::Magic Description

Associate user-defined magic to variables from Perl Magic is Perl way of enhancing objects. This mechanism lets the user add extra data to any variable and hook syntaxical 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's not surprising, as tied variables are implemented as a special kind of magic, just like any 'irregular' Perl variable : scalars like $!, $( or $^W, the %ENV and %SIG hashes, the @ISA array, vec() and substr() lvalues, threads::shared variables... They all share the same underlying C API, and this module gives you direct access to it.SYNOPSIS use Variable::Magic qw/wizard cast VMG_OP_INFO_NAME/; { # A variable tracer my $wiz = wizard set => sub { print "now set to ${$_}!\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 { $_ }, fetch => sub { $_ = $_ unless exists $_->{$_}; () }, store => sub { print "key $_ stored in $_\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", because the 'banana' key doesn't exist in %h $h{pear} = 1; # "key pear stored in helem" } Requirements: · Perl


Variable::Magic Related Software