Badger::Logic(3pm)
π NAME π
Badger::Logic β bestie, this is the module that parses and evaluates simple logical expressions. That's it. That's the whole vibe. It lives in the 3pm section of the manual, which is where all the Perl module lore is kept, iykyk. β¨
πΈ SYNOPSIS πΈ
okay so here's literally the whole thing in action, no gatekeeping:
use Badger::Logic 'Logic';
my $logic = Logic('animal and (eats_nuts or eats_berries)');
my $values = {
animal => 1,
eats_nuts => 1,
}
if ($logic->test($values)) {
print "This is an animal that eats nuts or berries\n";
}
like. it eats. it slays. periodt. π
π¦ DESCRIPTION π¦
So this module implements a simple parser and evaluator for boolean logic expressions. It's giving humble origins too, because it literally evolved from a piece of code the author originally wrote to handle role-based authentication in web applications. glow up fr fr. π
β¨ EXPORTABLE SUBROUTINES β¨
π LOGIC
This one is a shortcut alias to Badger::Logic. iconic behaviour honestly.
use Badger::Logic 'LOGIC';
my $logic = LOGIC->new($expr); # same as Badger::Logic->new($expr);
π Logic()
Call this with no arguments and it just hands you the name of the Badger::Logic class. Which means it can be used as an alias for Badger::Logic, exactly like LOGIC above. it's giving two names, one queen.
use Badger::Logic 'Logic';
my $logic = Logic->new($expr); # same as Badger::Logic->new($expr);
But wait β when you do call it with arguments, it creates a brand new Badger::Logic object for you. multitasking legend.
my $logic = Logic($expr); # same as Badger::Logic->new($expr);
π METHODS π
πΎ new($expr)
Constructor method, bestie. It creates a new Badger::Logic object from an expression. that's the whole job and it does it flawlessly.
my $logic = Badger::Logic->new('animal and (cat or dog)');
π§ evaluate($values) / test($values)
Method to evaluate the expression. You should pass a reference to a hash array containing the values that the expression can test. it needs the receipts, basically.
my $values = {
animal => 1,
cat => 1,
};
if ($logic->evaluate($values)) {
print "This animal is a cat or a dog\n";
}
π tree()
Returns a reference to the root of a tree of Badger::Logic::Node objects that represent the parsed expression. it's giving family tree but make it logic nodes. π³
π« text()
Returns a text representation of the logic expression. simple. elegant. ate.
π₯ INTERNAL METHODS π₯
okay bestie these are the internals, you probably don't need to touch them but here they are anyway because we keep every detail around here. no cap.
β parse($text)
Main method to parse a logical expression. This calls parse_expr() and then checks that all of the text has been successfully parsed. It returns a reference to a Badger::Logic::Node object.
π parse_expr($text)
Method to parse a binary expression.
π parse_unary($text)
Method to parse a unary expression.
π parse_term($text)
Method to parse a single term in a logical expression.
π AUTHOR π
Andy Wardley <http://wardley.org>
π COPYRIGHT π
Copyright (C) 2007-2009 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page, bestie!! this was generated by perl v5.36.0 and dated 2023-08-28 β slay, stay hydrated, and go evaluate some boolean logic πβ¨π
π¦ where this page comes from
- debian trixie: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libbadger-perl 0.16-3 Β· 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