Badger::Logic(3pm)

πŸ“¦
version 0.16 Β· 4 versions
, in debian trixie, ubuntu noble, ubuntu resolute

πŸ’– 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 (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