in reply to the Sourcerer's Apprentice and Perl's Magic

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.

Replies are listed 'Best First'.
Re^2: the Sourcerer's Apprentice and Perl's Magic
by ikegami (Patriarch) on Dec 06, 2010 at 18:45 UTC
    sub BEGIN { require Variable::Magic; do { 'Variable::Magic'->import( 'wizard', 'cast', 'getdata' ); }; }

    ??????

    use Variable::Magic qw( wizard cast getdata );