Declar...rators(3pm)

πŸ“¦
version 0.03 Β· 5 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Declar...rators(3pm) β€” User Contributed Perl Documentation πŸ’–

okay bestie, so this is the manual page for Declare::Constraints::Simple::Library::Operators, living in the User Contributed Perl Documentation section. it's giving "operators, but make it constraint-flavoured" and honestly? iconic.


✨ NAME ✨

Declare::Constraints::Simple::Library::Operators β€” Operators πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

no cap, here's the vibe:

# all hast to be valid
my $and_constraint = And( IsInt,
                          Matches(qr/0$/) );

# at least one has to be valid
my $or_constraint = Or( IsInt, HasLength );

# only one can be valid
my $xor_constraint = XOr( IsClass, IsObject );

# reverse validity
my $not_an_integer = Not( IsInt );

# case valid, validate 'bar' key depending on 'foo' keys value
my $struct_prof =
  And( IsHashRef,
       CaseValid( OnHashKeys(foo => IsEq("FooArray")),
                    OnHashKeys(bar => IsArrayRef),
                  OnHashKeys(foo => IsEq("FooHash")),
                    OnHashKeys(bar => IsHashRef) ));

like… look at her. main character energy. πŸ’…


🌸 DESCRIPTION 🌸

so this module contains the framework's operators. these constraint-like little guys act on the validity of the constraints you pass in β€” they don't check the value themselves, they check the checks, yk? 🫢


πŸ‘‘ OPERATORS πŸ‘‘

πŸ’œ And(@constraints)

true if all the passed @constraints are true on the value. and if it fails? it returns the result of the first failing constraint. so you get receipts on who broke it first, fr fr. 😭

πŸ“ Or(@constraints)

true if at least one of the passed @constraints is true. if it ends up false, you get back the result of the last failing constraint. slay. ✨

🧚 XOr(@constraints)

valid only if a single one of the passed @constraints is valid. not two. not zero. one. exclusive, as the name implies, periodt. πŸ’… if false, it returns the result of the last failing constraint.

🌈 Not($constraint)

valid if the passed $constraint is false. the whole point of this one is letting you easily flip a constraint's trueness β€” the reversion glow-up. πŸ’–

πŸŽ€ CaseValid($test, $conseq, $test2, $conseq2, ...)

okay this one is the plot twist. it runs every given $test argument on the value, one after another, until it finds one that returns true. if none of them hits, false is returned. but the moment one does hit true? the corresponding $conseq constraint gets applied to the value, and that result is what comes back. so you can validate depending on other properties of the value, like:

my $flexible = CaseValid( IsArrayRef,
                            And( HasArraySize(1,5),
                                 OnArrayElements(0 => IsInt) ),
                          IsHashRef,
                            And( HasHashElements(qw( head tail )),
                                 OnHashKeys(head => IsInt) ));

ngl you could probably model most of this with the other operators, but this way is a bit more readable, and we love readability in this house. πŸ πŸ’– for default cases, use ReturnTrue from Declare::Constraints::Simple::Library::General as the test. iykyk. 😌


πŸ”₯ SEE ALSO πŸ”₯

  • Declare::Constraints::Simple
  • Declare::Constraints::Simple::Library

go read those too bestie, they're the whole ecosystem. 🌱


πŸ’« AUTHOR πŸ’«

Robert 'phaylon' Sedlacek "<phaylon@dunkelheit.at>" β€” absolute legend, we stan. πŸ‘‘


this module is free software, you can redistribute it and/or modify it under the same terms as perl itself. so like, go wild (responsibly). 🫢


aaaand that's the whole page, babe! πŸ¦‹ this one came from perl v5.32.0, dated 2021-01-09, and it's filed under Declar...rators(3pm) in the User Contributed Perl Documentation. mwah, take care of her. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from