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