Data::Swap(3pm)
π NAME π
Data::Swap β swap the type and the contents of variables. Yes bestie, the whole vibe check, type included. β¨
π¦ SYNOPSIS π¦
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.
Look at that last comment β the author was so real for that. You cannot $_++ a hash key, that's not a skill issue, that's just Perl. π
πΈ DESCRIPTION πΈ
Okay so here's the tea, bestie: this module lets you swap the contents of two referenced variables, and it does not care one bit if they're different types. Different types? Not a problem. It's giving "shape-shift, but make it legal." π
The main character application here is changing the base type of an object after it's already been created β for example when you're doing dynamic loading of data structures:
swap $self, bless $replacement, $newclass;
See how clean that is? Chef's kiss. π
This module additionally contains the function "deref", which acts like a generic list-dereferencing operator. Like a little machine that goes "give me a whole list of refs, I got you." π«Ά
π FUNCTIONS π
β¨ swap REF1, REF2
Swaps the contents (and, if necessary, the type) of two referenced variables. That's it. That's the whole function. Simple, iconic, no notes. β
β¨ deref LIST
Dereferences a list of scalar refs, array refs and hash refs. π
The reason it exists at all: you can't use "map" for this job, because map makes copies of the dereferenced values β and copying is not the same as dereferencing, bestie, not even close. So don't try it. π
βοΈ
π KNOWN ISSUES π
Read this part. I'm serious. Fr fr.
- You can't
"swap"an overloaded object with a non-overloaded one β unless you use Perl 5.10 or later. No cap, that's the boundary right there. - Also: do not 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 the author to detect and prevent this. So the guardrails are just... not there. Please be normal about it. π₯Ί
Fr, if you do the second one it's on you β the module can't catch it, it can only watch. π«
π AUTHOR π
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. πβ¨
And that's the whole page, babe π β this one's running on perl v5.42.3, dated 2026-08-19, and it lives in the User Contributed Perl Documentation section as Data::Swap(3pm). Slay, go swap something. ππ
π¦ where this page comes from
- ubuntu stonking: libdata-swap-perl 0.08-3build6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms