Declar...encial(3pm)

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

πŸ’– Declare::Constraints::Simple::Library::Referencial β€” Ref Constraints πŸ’–

okay bestie, so this is a page from the User Contributed Perl Documentation collection, and it's literally all about checking whether your refs are valid and what kind of refs they are. no cap. it's giving "who even are you, reference?" energy and i'm obsessed. πŸ¦‹


✨ NAME ✨

Declare::Constraints::Simple::Library::Referencial β€” Ref Constraints. that's it, that's the whole name. say it three times fast, i dare you. πŸ’…


🌸 SYNOPSIS 🌸

here's the vibe, in code, exactly as the ancestors wrote it:

# scalar or array references
my $scalar_or_array = IsRefType( qw(SCALAR ARRAY) );

# scalar reference
my $int_ref = IsScalarRef( IsInt );

# accept mappings of ids to objects with "name" methods
my $id_obj_map =
  IsHashRef( -keys   => IsInt,
             -values => And( IsObject,
                             HasMethods('name') ));

# an integer list
my $int_list = IsArrayRef( IsInt );

# accept code references
my $is_closure = IsCodeRef;

# accept a regular expression
my $is_regex = IsRegex;

like… look at her. she's beautiful. she checks ALL the refs. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so this library is where all the constraints live that test the validity of references and their types. if you've got a ref and you're like "girl, what ARE you," this is the library that answers. periodt.


πŸ‘‘ CONSTRAINTS πŸ‘‘

🌈 IsRefType(@types)

valid if the value is a reference of a kind that's in @types. that's the whole check. simple, iconic, no notes.

πŸ’œ IsScalarRef($constraint)

this one's true if the value is a scalar reference. and get this β€” you can pass a possible constraint for the scalar reference's target value too. e.g.

my $test_integer_ref = IsScalarRef(IsInt);

so like, not just "is it a scalar ref" but "is it a scalar ref holding an int." main character behavior fr fr.

⭐ IsArrayRef($constraint)

the value is valid if the value is an array reference. and you can validate the contents of the array by passing another $constraint as an argument. slay.

the stack or path part of IsArrayRef is IsArrayRef[$index] where $index is the index of the failing element. so when something goes wrong, she tells you exactly which one. queen behavior. 🫢

πŸ“ IsHashRef(-keys => $constraint, -values => $constraint)

true if the value is a hash reference. it can also take two named parameters:

  • -keys can pass a constraint to check the hash's keys
  • -values does the same for its values

the stack or path part of IsHashRef looks like IsHashRef[$type $key] where $type is either "val" or "key" depending on what was validated, and $key being the key that didn't pass validation. she names names. πŸ’€

🧚 IsCodeRef()

code references have to be valid to pass this constraint. that's the bar. clear it or don't. ✨

🌸 IsRegex()

true if the value is a regular expression built with qr.

⚠️ note however, that a simple string that could be used like /$rx/ will not pass this constraint. i know. devastating. 😭

you can combine multiple constraints with And(@constraints) though, so it's not over, bestie. we adapt. πŸ’…


πŸ”₯ SEE ALSO πŸ”₯

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

go read those too, iykyk. πŸ“š


πŸ’– AUTHOR πŸ’–

Robert 'phaylon' Sedlacek <phaylon@dunkelheit.at>


this module is free software, you can redistribute it and/or modify it under the same terms as perl itself. go off, legally. 🫢


and that's the whole page, bestie!! this beauty shipped with perl v5.32.0, dated 2021-01-09, and yeah β€” the header and footer said the same thing three times, which is so on brand. mwah. πŸ’‹βœ¨πŸŒΈ

πŸ“¦ where this page comes from