This little meditation is just to raise awareness of a very interesting little module I came across while on the Gtk2::Perl maillist. I'm not an expert at all at Perl internals, but there is a new module called Variable::Magic.

From the README:

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'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.


I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh
  • Comment on the Sourcerer's Apprentice and Perl's Magic

Replies are listed 'Best First'.
Re: the Sourcerer's Apprentice and Perl's Magic
by ikegami (Patriarch) on Dec 03, 2010 at 19:26 UTC
    Thanks. One could say that tie and Variable::Magic are both Perl interfaces to magic. The latter is more complete (allowing you to do more low-level stuff) and possibly lighter (especially if you don't need to override both setting and fetching).
Re: the Sourcerer's Apprentice and Perl's Magic
by Khen1950fx (Canon) on Dec 06, 2010 at 13:11 UTC
    zentara++. Perl's magic is one of the things that got me hooked on Perl, so I decided to give it a try. Using "Hello, world!", I tried a peek at a tie that I did, and then I used an example from Variable::Magic. Interesting...
    #!/usr/bin/perl use strict; use warnings; use Devel::Peek; use Variable::Magic qw( wizard cast getdata ); my $b = 'Hello, world!'; my $a = bless( do { \$b; }, 'main' ); print Dump($a); print "===========================================\n"; package Magical::UserData; sub BEGIN { require Variable::Magic; do { 'Variable::Magic'->import( 'wizard', 'cast', 'getdata' ); }; } package main; { package Magical::UserData; my $wiz = wizard( 'data', sub { \$_[1]; } ); sub ud (\[$@%*&]) : lvalue { my ($var) = @_; my $data = &getdata( $var, $wiz ); unless ( defined $data ) { $data = \my ($slot); die q[Couldn't cast UserData magic onto the variable] unless &cast( $var, $wiz, $slot ); } $$data; } } sub BEGIN { *ud = \&Magical::UserData::ud; } { my $cb; $cb = sub { print 'Hello, ', &ud( \&$cb ), "!\n"; }; &ud( \&$cb ) = 'world'; print Dump( &$cb() ); }
    Update: Fixed line 5. Thanks, ikegami.
      sub BEGIN { require Variable::Magic; do { 'Variable::Magic'->import( 'wizard', 'cast', 'getdata' ); }; }

      ??????

      use Variable::Magic qw( wizard cast getdata );
Re: the Sourcerer's Apprentice and Perl's Magic
by zentara (Cardinal) on Dec 06, 2010 at 17:42 UTC
    Nice example. I'm wondering if the variable magic concept could be used to extend the types to be anything you want to define, and have handlers for it? For instance, how about a PhP or Python data type, which would signal the interpretor to run it via another built-in interpretor.... thus giving a neat way to encapsulate foreign code into Perl

    one interpretor, runs all languages .... the tower of babel language :-)


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh