Tie::H...rdered(3pm)

πŸ“¦
version 0.006 Β· 2 versions
  • 0.006 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.006 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ NAME ✨

Tie::Hash::MultiValueOrdered β€” it's a hash, bestie, but make it extra: multiple values per key, and the keys stay in order πŸ’…


🌸 SYNOPSIS 🌸

use Test::More;
use Tie::Hash::MultiValueOrdered;

my $tied = tie my %hash, "Tie::Hash::MultiValueOrdered";

$hash{a} = 1;
$hash{b} = 2;
$hash{a} = 3;
$hash{b} = 4;

# Order of keys is predictable
is_deeply(
   [ keys %hash ],
   [ qw( a b ) ],
);

# Order of values is predictable
# Note that the last values of 'a' and 'b' are returned.
is_deeply(
   [ values %hash ],
   [ qw( 3 4 ) ],
);

# Can retrieve list of all key-value pairs
is_deeply(
   [ $tied->pairs ],
   [ qw( a 1 b 2 a 3 b 4 ) ],
);

# Switch the retrieval mode for the hash.
$tied->fetch_first;

# Now the first values of 'a' and 'b' are returned.
is_deeply(
   [ values %hash ],
   [ qw( 1 2 ) ],
);

# Switch the retrieval mode for the hash.
$tied->fetch_list;

# Now arrayrefs are returned.
is_deeply(
   [ values %hash ],
   [ [1,3], [2,4] ],
);

# Restore the default retrieval mode for the hash.
$tied->fetch_last;

done_testing;

πŸ’– DESCRIPTION πŸ’–

okay so picture a normal hash. now tie it to this class. it acts more or less like a standard hash, right? except β€” and this is the whole moment β€” when you assign a new value to a key that already exists, the old value doesn't get yeeted. it stays underneath, chillin'. iconic behavior, no cap πŸ¦‹

but when you explicitly delete a key? that nukes all the values hanging off that key. gone. periodt. πŸ’€

by default those old values are like… not reachable through the hash interface itself (they're lowkey hidden), but you can get at them through the tied object:

my @values = tied(%hash)->get($key);

and if you wanna actually change how the hash behaves? that's what all the fetch_* methods are for. they let you switch up the retrieval vibe. πŸŽ€

🫢 Tied Object Methods 🫢

  • pairs β€” hands you every key-value pair the hash has (duplicates included!!) as one flattened list. yes, all of them. ✨
  • pair_refs β€” same energy, but as a list of two-item arrayrefs instead. cute little bundles. πŸŽ€
  • get($key) β€” returns the whole list of values tied to one key. not just one. the list. πŸ’…
  • keys β€” all the hash keys in their original order. if a key shows up more than once, you only get the first occurrence.
  • rr_keys β€” all the hash keys in their original order, but for duplicates you get the last occurrence instead.
  • all_keys β€” all the hash keys in their original order, duplicates and all. no filtering, we're keeping everything. 😌
  • values β€” the values that go with keys.
  • rr_values β€” the values that go with rr_keys.
  • all_values β€” the values that go with all_keys.

🌈 Fetch Styles 🌈

  • fetch_last β€” this is the default style of fetching, bestie. the one you get for free.
tie my %hash, "Tie::Hash::MultiValueOrdered";

$hash{a} = 1;
$hash{b} = 2;
$hash{b} = 3;

tied(%hash)->fetch_last;

is($hash{a}, 1);
is($hash{b}, 3);
  • fetch_first β€” same setup, opposite energy. first one wins. πŸ’–
tie my %hash, "Tie::Hash::MultiValueOrdered";

$hash{a} = 1;
$hash{b} = 2;
$hash{b} = 3;

tied(%hash)->fetch_first;

is($hash{a}, 1);
is($hash{b}, 2);
  • fetch_list β€” now you're getting arrayrefs back, because why pick one when you can have them all. πŸ‘‘
tie my %hash, "Tie::Hash::MultiValueOrdered";

$hash{a} = 1;
$hash{b} = 2;
$hash{b} = 3;

tied(%hash)->fetch_first;

is_deeply($hash{a}, [1]);
is_deeply($hash{b}, [2, 3]);
  • fetch_iterator β€” > heads up, this fetch style is experimental and subject to change. don't get too attached, fr fr. πŸ™ˆ
tie my %hash, "Tie::Hash::MultiValueOrdered";

$hash{a} = 1;
$hash{b} = 2;
$hash{b} = 3;

tied(%hash)->fetch_iterator;

my $A = $hash{a};
my $B = $hash{b};

is($A->(), 1);
is($B->(), 2);
is($B->(), 3);

πŸ’€ BUGS πŸ’€

found a bug? not the vibe, but please report it here:

<http://rt.cpan.org/Dist/Display.html?Queue=JSON-MultiValueOrdered>


πŸ¦‹ SEE ALSO πŸ¦‹

  • JSON::Tiny::Subclassable
  • JSON::Tiny
  • Mojo::JSON

πŸ‘‘ AUTHOR πŸ‘‘

Toby Inkster <tobyink@cpan.org>. (the author. the main character. we love.) πŸ’œ


This software is copyright (c) 2012-2013 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


🧚 DISCLAIMER OF WARRANTIES 🧚

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

translation: no promises, bestie, it's exactly what it says on the tin 😭

okay that's the whole page!! this was the Tie::Hash::MultiValueOrdered manual, coming to you from the User Contributed Perl Documentation, perl v5.36.0, dated 2022-11-20 β€” mwah, go forth and tie some hashes πŸ’…βœ¨

πŸ“¦ where this page comes from