Declar...y::Base(3pm)
π Declar...y::Base(3pm) β the User Contributed Perl Documentation moment π
okay bestie, gather round, because this is Declare::Constraints::Simple::Library::Base and it is literally the blueprint every other library in this family builds on. it's the base class, the mom friend, the one holding everybody's stuff together. no cap. β¨
πΈ NAME πΈ
Declare::Constraints::Simple::Library::Base β the Library Base Class, aka the foundational vibe π
π¦ SYNOPSIS π¦
here's the whole aesthetic, verbatim, don't touch a character:
package My::Constraint::Library;
use warnings;
use strict;
# this installs the base class and helper functions
use Declare::Constraints::Simple-Library;
# we can also automagically provide other libraries
# to the importer
use base 'Declare::Constraints::Simple::Library::Numericals';
# with this we define a constraint to check a value
# against a serial number regular expression
constraint 'SomeSerial',
sub {
return sub {
return _true if $_[0] =~ /\d{3}-\d{3}-\d{4}/;
return _false('Not in SomeSerial format');
};
};
1;
see how clean that is? that's the energy. that's the assignment. π«Ά
β¨ DESCRIPTION β¨
so like, this base class is where all the common library functionality lives. we're talking helper functions and the install mechanisms β the whole infrastructure. everything else gets to just exist on top of it, which is honestly main character behavior of the supportive kind. π
π METHODS π
install_into($target)
installs the base classes and helper functions into the $target namespace. the %CONSTRAINT_GENERATORS package variable of that class is what gets used as storage for its constraints. that's the closet. that's where the fits live. π
fetch_constraint_declarations()
class method. hands you back all the constraints registered to the class. the whole collection. no gatekeeping. π
fetch_constraint_generator($name)
class method. returns the constraint generator code reference registered under $name. and heads up bestie β if that generator can't be found, this call will raise a "croak". it's not being dramatic, it's being correct. π
prepare_generator($constraint_name, $generator)
class method. this wraps the $generator in a closure that provides stack and failure-collapsing decisions. it's giving backstage pass energy β all the messy logistics handled before the performance. β
add_constraint_generator($name, $code)
class method. this is the actual registration method, and it's the one "constraint" uses under the hood. the real one. the plug. π₯
π HELPER FUNCTIONS π
okay so quick note before we dive in: some of these helper functions have an "_" prefix. and yes, that technically means they're internal functions β but it is totally ok to call them, because they have a fixed API. they're not distribution-internal, they're library-internal. so the only intended audience is code running inside constraints. iykyk. π
constraint($name, $code)
constraint 'Foo', sub { ... };
this registers a new constraint in the calling library. important detail though β constraints have to return result objects. to make that happen you can use the helper functions "_result($bool, $msg", _true() and _false($msg). yes we're all using the helpers. it's a group project and we're all participating. π
_result($bool, $msg)
returns a new result object. its validity flag depends on the $bool argument. and the $msg argument is the error message to use on failure. so basically: you decide if it's a slay or a fail, and you write the note. π
_false($msg)
returns a non-valid result object, with its message set to $msg. we love a clear rejection. πΈ
_true()
returns a valid result object. that's it. that's the whole thing. and honestly? iconic. β¨
_info($info)
sets the current failure info to use in the stack info part. π§
_apply_checks($value, \@constraints, [$info])
this applies all constraints in the "\@constraints" array reference to the passed $value. and optionally you can specify an $info string, which gets used in the stack of the newly created non-valid results. so you can label your sadness. thoughtful! π
_listify($value)
puts $value into an array reference and returns it β but only if it isn't already one. so if it's already an arrayref, it just leaves it alone. we do not double-wrap. we have boundaries. π
_with_message($msg, $closure, @args)
this is the internal version of the general "Message" constraint. it sets the current overridden message to $msg and then executes the $closure with @args as arguments. π
_with_scope($scope_name, $constraint, @args)
applies the $constraint to @args in a newly created scope named by $scope_name. new scope, who dis. π¦
_set_result($scope, $name, $result)
stores the given $result unter the name $name in $scope. (that's "unter" as in the original, bestie β we don't correct the source, we honor it. π
)
_get_result($scope, $name)
returns the result named $name from $scope. π«Ά
_has_result($scope, $name)
returns true only if such a result was registered already. a vibe check, essentially. π
π₯ SEE ALSO π₯
Declare::Constraints::SimpleDeclare::Constraints::Simple::Library
go read those too, bestie. they're the extended universe. β
π AUTHOR π
Robert 'phaylon' Sedlacek "<phaylon@dunkelheit.at>" β thank you for your service, king. π
π LICENSE AND COPYRIGHT π
this module is free software, you can redistribute it and/or modify it under the same terms as perl itself. periodt. π
okay that's the whole page!! and just so you have the receipts at the very end: this was perl v5.32.0, dated 2021-01-09, from Declar...y::Base(3pm). slay, go write some constraints, love u 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