Created
October 25, 2013 05:23
-
-
Save bowman/7149766 to your computer and use it in GitHub Desktop.
Perl Variable::Magic experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -l | |
package A; | |
use strict qw(subs); | |
use warnings; | |
no warnings 'uninitialized'; | |
use Variable::Magic qw(wizard cast VMG_OP_INFO_NAME); | |
my $wiz; | |
BEGIN { | |
$wiz = wizard | |
data => sub { | |
my ($var, $tag) = @_; | |
my $r = ref $var; | |
if ($r eq 'ARRAY') { | |
print "data ARRAY @_ -- @$_[0]"; | |
&cast((ref() ? $_ : \$_), $wiz, $tag) for @$var; | |
} elsif ($r eq 'HASH') { | |
print "data HASH @_ -- " . join ',', %{$_[0]}; | |
&cast((ref() ? $_ : \$_), $wiz, $tag) for values %$var; | |
} elsif ($r eq 'SCALAR') { | |
print "data SCALAR @_ -- $$_[0]"; | |
#&cast((ref() ? $_ : \$_), $wiz, $tag) for $var; | |
} | |
return $tag; | |
}, | |
get => sub { print "get @_" }, | |
set => sub { print "set @_" }, | |
fetch => sub { print "fetch @_" }, | |
store => sub { print "store to @_" }, | |
copy => sub { print "copy @_" }, | |
op_info => VMG_OP_INFO_NAME; | |
cast %{A::}, $wiz, 'A::'; | |
} | |
cast $x, $wiz, '$x'; | |
cast %B, $wiz, '%B'; | |
$x = 123; | |
@y = (4,5,6); | |
$B{a} = 'a'; | |
print "$x @y\n"; | |
#print for grep { /^\w+$/ } keys (%{'A::'}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment