View on
MetaCPAN
David Helkowski > Set-Definition-0.01 > Set::Definition

Download:
Set-Definition-0.01.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  1
View/Report Bugs
Module Version: 0.01   Source  

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:

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:

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.
syntax highlighting: