Tie::A...::Lazy(3pm)

πŸ“¦
version 1.41 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

✨ NAME ✨

Tie::Array::Sorted::Lazy β€” it's an array, bestie, and it stays sorted πŸ’…


🌸 SYNOPSIS 🌸

use Tie::Array::Sorted::Lazy;

tie @a, "Tie::Array::Sorted::Lazy", sub { $_[0] <=> $_[1] };

push @a, 10, 4, 7, 3, 4;
print "@a"; # "3 4 4 7 10"

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so picture Tie::Array::Sorted but with main character energy for a very specific situation: arrays where you're writing way more than you're reading. Like, you're pushing stuff in constantly and hardly ever peeking at it. That's the vibe this module was built for πŸ’–

The way it works is genuinely iconic: instead of re-sorting the array every single time you insert something, it chills and lets the sorting happen on retrieval instead. So the work gets deferred until the moment you actually go looking at the array. Slay.

And the extra smart part? It only re-sorts if the data has actually been modified since the last sort. So if nothing changed and you fetch twice, the second fetch is just... free. No wasted effort, no drama. Periodt ✨

Here's another way you might set it up, comparing by file size with the -s operator:

tie @a, "Tie::Array::Sorted::Lazy", sub { -s $_[0] <=> -s $_[1] };

and that's the whole page, bestie 🫢 this one's from the User Contributed Perl Documentation, running on perl v5.36.0 and dated 2022-10-15 β€” go keep your arrays cute and sorted πŸ’œ

πŸ“¦ where this page comes from