]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - samples/magic.pl
5ce53ae49656ea1d8eee5976f99fd71e1dfeebc5
[perl/modules/Variable-Magic.git] / samples / magic.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use lib qw{blib/arch blib/lib};
7 use Variable::Magic qw/wizard cast dispell/;
8
9 sub foo { print STDERR "got ${$_[0]}!\n" }
10 my $bar = sub { ++${$_[0]}; print STDERR "now set to ${$_[0]}!\n"; };
11
12 my $a = 1;
13 {
14  my $wiz = wizard get  => \&foo,
15                   set  => $bar,
16                   free => sub { print STDERR "deleted!\n"; };
17  cast $a, $wiz, qw/a b c/;
18  ++$a;              # "got 1!", "now set to 3!"
19  dispell $a, $wiz;
20  cast $a, $wiz;
21  my $b = 123;
22  cast $b, $wiz;
23 }                   # "deleted!"
24 my $b = $a;         # "got 3!"
25 $a = 3;             # "now set to 4!"
26 $b = 3;             # (nothing)