File: 106_magic.t

package info (click to toggle)
libtie-hash-indexed-perl 0.08%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: makefile: 10; perl: 10
file content (49 lines) | stat: -rw-r--r-- 979 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
################################################################################
#
# Copyright (c) Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################

use Test;

BEGIN { plan tests => 10 };

use Tie::Hash::Indexed;
ok(1);

tie my $c, 'MagicScalar', 42;

ok($MagicScalar::GLOBAL, 42);
ok($c, 42);
$c = 13;
ok($MagicScalar::GLOBAL, 13);
ok($c, 13);

my $h = Tie::Hash::Indexed->new(foo => 1, bar => 2, zoo => 3, baz => 4);

$h->set('foo', $c);
ok($h->get('foo'), 13);

$h->add('foo', ++$c);
ok($h->get('foo'), 27);

my %h2;
$h2{foo} = $c;
ok($h2{foo}, 14);

$c++;
ok($h2{foo}, 14);

tie $h2{foo}, 'MagicScalar';
ok($h2{foo}, 15);

package MagicScalar;

use vars qw( $GLOBAL );

sub TIESCALAR { $GLOBAL = $_[1] if @_ > 1; bless [], $_[0] }
sub FETCH { $GLOBAL }
sub STORE { $GLOBAL = $_[1] }