GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Data::Swap(3) User Contributed Perl Documentation Data::Swap(3)

Data::Swap - Swap type and contents of variables

    use Data::Swap;

    my $p = [];
    my $q = {};
    print "$p $q\n";            # ARRAY(0x965cc) HASH(0x966b0)
    swap $p, $q;                # swap referenced variables
    print "$p $q\n";            # HASH(0x965cc) ARRAY(0x966b0)

    my $x = {};
    my $y = $x;                 # $x and $y reference same var
    swap $x, [1, 2, 3];         # swap referenced var with an array
    print "@$y\n";              # 1 2 3

    use Data::Swap 'deref';

    my @refs = (\$x, \@y);
    $_++ for deref @refs;       # dereference a list of references

    # Note that I omitted \%z from the @refs because $_++ would fail 
    # on a key, but deref does work on hash-refs too of course.

This module allows you to swap the contents of two referenced variables, even if they have different types.

The main application is to change the base type of an object after it has been created, for example for dynamic loading of data structures:

    swap $self, bless $replacement, $newclass;

This module additionally contain the function "deref" which acts like a generic list-dereferencing operator.

Swaps the contents (and if necessary, type) of two referenced variables.

Dereferences a list of scalar refs, array refs and hash refs. Mainly exists because you can't use "map" for this application, as it makes copies of the dereferenced values.

You can't "swap" an overloaded object with a non-overloaded one, unless you use Perl 5.10 or later.

Also, don't use "swap" to change the type of a directly accessible variable -- like "swap \$x, \@y". That's just asking for segfaults. Unfortunately there is no good way for me to detect and prevent this.

Matthijs van Duin <xmath@cpan.org>

Copyright (C) 2003, 2004, 2007, 2008 Matthijs van Duin. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

2008-09-12 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.