| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Data::Swap; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Data::Swap - Swap type and contents of variables |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Data::Swap; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $p = []; |
|
12
|
|
|
|
|
|
|
my $q = {}; |
|
13
|
|
|
|
|
|
|
print "$p $q\n"; # ARRAY(0x965cc) HASH(0x966b0) |
|
14
|
|
|
|
|
|
|
swap $p, $q; # swap referenced variables |
|
15
|
|
|
|
|
|
|
print "$p $q\n"; # HASH(0x965cc) ARRAY(0x966b0) |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $x = {}; |
|
18
|
|
|
|
|
|
|
my $y = $x; # $x and $y reference same var |
|
19
|
|
|
|
|
|
|
swap $x, [1, 2, 3]; # swap referenced var with an array |
|
20
|
|
|
|
|
|
|
print "@$y\n"; # 1 2 3 |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use Data::Swap 'deref'; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my @refs = (\$x, \@y); |
|
25
|
|
|
|
|
|
|
$_++ for deref @refs; # dereference a list of references |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Note that I omitted \%z from the @refs because $_++ would fail |
|
28
|
|
|
|
|
|
|
# on a key, but deref does work on hash-refs too of course. |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This module allows you to swap the contents of two referenced variables, even |
|
33
|
|
|
|
|
|
|
if they have different types. |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
The main application is to change the base type of an object after it has been |
|
36
|
|
|
|
|
|
|
created, for example for dynamic loading of data structures: |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
swap $self, bless $replacement, $newclass; |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
This module additionally contain the function C which acts like a |
|
41
|
|
|
|
|
|
|
generic list-dereferencing operator. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 swap I, I |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Swaps the contents (and if necessary, type) of two referenced variables. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 deref I |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Dereferences a list of scalar refs, array refs and hash refs. Mainly exists |
|
52
|
|
|
|
|
|
|
because you can't use C |
|
53
|
|
|
|
|
|
|
dereferenced values. |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 KNOWN ISSUES |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
You can't C an overloaded object with a non-overloaded one, |
|
58
|
|
|
|
|
|
|
unless you use Perl 5.10 or later. |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Also, don't use C to change the type of a directly accessible |
|
61
|
|
|
|
|
|
|
variable -- like C. That's just asking for segfaults. |
|
62
|
|
|
|
|
|
|
Unfortunately there is no good way for me to detect and prevent this. |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 AUTHOR |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Matthijs van Duin |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Copyright (C) 2003, 2004, 2007, 2008 Matthijs van Duin. |
|
69
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
|
70
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
|
73
|
|
|
|
|
|
|
|
|
74
|
2
|
|
|
2
|
|
30464
|
use 5.006; |
|
|
2
|
|
|
|
|
8
|
|
|
|
2
|
|
|
|
|
77
|
|
|
75
|
|
|
|
|
|
|
|
|
76
|
2
|
|
|
2
|
|
12
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
63
|
|
|
77
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
103
|
|
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
|
80
|
|
|
|
|
|
|
|
|
81
|
2
|
|
|
2
|
|
8
|
use base 'Exporter'; |
|
|
2
|
|
|
|
|
10
|
|
|
|
2
|
|
|
|
|
211
|
|
|
82
|
2
|
|
|
2
|
|
9
|
use base 'DynaLoader'; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
266
|
|
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
our @EXPORT = qw(swap); |
|
85
|
|
|
|
|
|
|
our @EXPORT_OK = qw(swap deref); |
|
86
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => \@EXPORT_OK); |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
bootstrap Data::Swap $VERSION; |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |