Skip to content

Instantly share code, notes, and snippets.

@bowman
Created October 25, 2013 05:23
Show Gist options
  • Save bowman/7149766 to your computer and use it in GitHub Desktop.
Save bowman/7149766 to your computer and use it in GitHub Desktop.
Perl Variable::Magic experiment
#!/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