Declar...y::Hash(3pm)
π Declare::Constraints::Simple::Library::Hash β hash constraints, bestie π
okay so this is a page from the User Contributed Perl Documentation (that's the manual name, it's giving very "community-made, we love her") and it's all about one module: Declare::Constraints::Simple::Library::Hash. Say that five times fast. Or don't. I didn't.
β¨ NAME β¨
Declare::Constraints::Simple::Library::Hash β literally just Hash Constraints. That's it. That's the whole vibe. π
π¦ SYNOPSIS π¦
my $constraint = And(
# make sure all keys are present
HasAllKeys( qw(foo bar) ),
# constraints for the keys
OnHashKeys( foo => IsInt, bar => HasLength )
);
likeβ¦ look at her. combining constraints with And. obsessed. π
πΈ DESCRIPTION πΈ
This module is where all the constraints that get applied to hash references live. Every single one. They're all here. no cap.
π HasAllKeys(@keys)
For this one to pass, your value has to be a hashref and it has to contain every single key listed in @keys. Miss one? Rejected. Sorry bestie. π
The stack or path part of HasAllKeys is:
HasAllKeys[$key]
where $key is the missing key. So when it fails, it tells you exactly which one ghosted you. iconic behaviour, honestly.
π§ OnHashKeys(key => $constraint, key => $constraint, ...)
This one lets you hand over a constraint for each specific key in a hash reference. Very bespoke. Very curated. β¨
heads up: if a key you specified is not in the validated hash reference, the validation for that key justβ¦ doesn't happen. It's not an error, it just gets skipped. π
So if you actually need that key to be there, you gotta make it a requirement by pairing HasAllKeys(@keys) (from above β¬οΈ) with this one, like:
And( HasAllKeys( qw(foo bar baz) )
OnHashKeys( foo => IsInt,
bar => Matches(qr/bar/),
baz => IsArrayRef( HasLength )));
and as you might've clocked already β you do not have to check for IsHashRef validity in here. The hash constraints are already doing that by themselves. they're self-sufficient queens. π
The stack or path part of OnHashKeys looks like:
OnHashKeys[$key]
where $key is the key of the failing value. so you always know who's the problem. π
π SEE ALSO π
Declare::Constraints::SimpleDeclare::Constraints::Simple::Library
go read those too, they're part of the same friend group π«Ά
π AUTHOR π
Robert 'phaylon' Sedlacek <phaylon@dunkelheit.at> β the mastermind. thank you for your service. π«‘
π LICENSE AND COPYRIGHT π
This module is free software, you can redistribute it and/or modify it under the same terms as perl itself. slay, and also legally binding. β¨
and that's the whole page, bestie!! perl v5.32.0, dated 2021-01-09, brought to you by the Declar...y::Hash(3pm) crew. mwah. π bye!! π¦π
π¦ where this page comes from
- debian bookworm: libdeclare-constraints-simple-perl 0.03-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libdeclare-constraints-simple-perl 0.03-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libdeclare-constraints-simple-perl 0.03-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdeclare-constraints-simple-perl 0.03-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdeclare-constraints-simple-perl 0.03-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdeclare-constraints-simple-perl 0.03-1.1 Β· 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