| 1 |
Data::Swap 0.08 |
| 2 |
|
| 3 |
To install this module type the following: |
| 4 |
|
| 5 |
perl Makefile.PL |
| 6 |
make |
| 7 |
make test |
| 8 |
make install |
| 9 |
|
| 10 |
|
| 11 |
Module documentation: |
| 12 |
|
| 13 |
NAME |
| 14 |
Data::Swap - Swap type and contents of variables |
| 15 |
|
| 16 |
SYNOPSIS |
| 17 |
use Data::Swap; |
| 18 |
|
| 19 |
my $p = []; |
| 20 |
my $q = {}; |
| 21 |
print "$p $q\n"; # ARRAY(0x965cc) HASH(0x966b0) |
| 22 |
swap $p, $q; # swap referenced variables |
| 23 |
print "$p $q\n"; # HASH(0x965cc) ARRAY(0x966b0) |
| 24 |
|
| 25 |
my $x = {}; |
| 26 |
my $y = $x; # $x and $y reference same var |
| 27 |
swap $x, [1, 2, 3]; # swap referenced var with an array |
| 28 |
print "@$y\n"; # 1 2 3 |
| 29 |
|
| 30 |
use Data::Swap 'deref'; |
| 31 |
|
| 32 |
my @refs = (\$x, \@y); |
| 33 |
$_++ for deref @refs; # dereference a list of references |
| 34 |
|
| 35 |
# Note that I omitted \%z from the @refs because $_++ would fail |
| 36 |
# on a key, but deref does work on hash-refs too of course. |
| 37 |
|
| 38 |
DESCRIPTION |
| 39 |
This module allows you to swap the contents of two referenced variables, |
| 40 |
even if they have different types. |
| 41 |
|
| 42 |
The main application is to change the base type of an object after it |
| 43 |
has been created, for example for dynamic loading of data structures: |
| 44 |
|
| 45 |
swap $self, bless $replacement, $newclass; |
| 46 |
|
| 47 |
This module additionally contain the function "deref" which acts like a |
| 48 |
generic list-dereferencing operator. |
| 49 |
|
| 50 |
FUNCTIONS |
| 51 |
swap REF1, REF2 |
| 52 |
Swaps the contents (and if necessary, type) of two referenced variables. |
| 53 |
|
| 54 |
deref LIST |
| 55 |
Dereferences a list of scalar refs, array refs and hash refs. Mainly |
| 56 |
exists because you can't use "map" for this application, as it makes |
| 57 |
copies of the dereferenced values. |
| 58 |
|
| 59 |
KNOWN ISSUES |
| 60 |
You can't "swap" an overloaded object with a non-overloaded one, unless |
| 61 |
you use Perl 5.10 or later. |
| 62 |
|
| 63 |
Also, don't use "swap" to change the type of a directly accessible |
| 64 |
variable -- like "swap \$x, \@y". That's just asking for segfaults. |
| 65 |
Unfortunately there is no good way for me to detect and prevent this. |
| 66 |
|
| 67 |
AUTHOR |
| 68 |
Matthijs van Duin <[email protected]> |
| 69 |
|
| 70 |
Copyright (C) 2003, 2004, 2007, 2008 Matthijs van Duin. This program is |
| 71 |
free software; you can redistribute it and/or modify it under the same |
| 72 |
terms as Perl itself. |