The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Set::Definition - Class to handle simple logical set unions and intersections.

VERSION

0.01

SYNOPSIS

use Set::Definition;

my $hash = { a => [ 1, 2, 3 ], b => [ 2, 3, 5 ] };
my $set = Set::Definition->new( text => "a & b", ingroup_callback => \&in_group, hash => $hash );

my $members = $set->members(); # will be [ 2, 3 ]
my $has2 = $set->contains( 2 ); # will be true
my $has5 = $set->contains( 5 ); # will be false

sub in_group {
  my ( $group_name, $item, $options ) = @_;
  my $hash = $options->{'hash'};
  my $gp = $hash->{ $group_name };
  if( !$item ) {
      return $gp if( defined $gp );
      return [];
  }
  for my $member ( @$gp ) {
      return 1 if( $member eq $item );
  }
}

DESCRIPTION

Set::Definition allows you to define a logical set that contains members by way your own custom function that checks if an item is a member of a named set, and/or gives the list of all items in a named set.

A boolean expression is accepted when creating the set, which defines how the named sets should be joined together logically.

Any additional parameters passed during construction will be passed to your callback function in the 'options' hash. Eg:

my $set = Set::Definition->new( text => 'a & b', ingroup_callback => \&your_func, [parameters to go in options] );

sub your_func {
  my ( $group_name, $item, $options ) = @_;
  # options is now a hash containing your parameters
}

Accepted Boolean Expressions

The expression accepted can contains parantheses, as well as the '!' character to respresent set exclusion. All of the following are valid expressions:

  • apples | oranges

  • fruits & red

  • fruits & red & !apples

  • ( flying | birds ) & !airplanes

  • ( a & b & ( c | d ) ) | ( f & !e )

Using contains() with members()

Note that some expressions may work for the 'contains', but it may not make sense to call the members() function. Example:

  • !a ( unless you have decided a domain, how do you know everything not in a )

LICENSE

Copyright (C) 2013 David Helkowski

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.  You may also can
redistribute it and/or modify it under the terms of the Perl
Artistic License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.