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::Groups - A set of groups.

SYNOPSIS

  use Set::Groups ;

  # create a set of groups
  $groups = new Set::Groups ;
  
  # create a group MyGroup with a single member
  $groups->addSingleTo("single1","MyGroup") ;

  # add 2 singles members into MyGroup
  $groups->addSinglesTo(["single2","single3"],"MyGroup") ;
  
  # add a group member into MyGroup
  $groups->addGroupTo("Member1Group","MyGroup") ; 

  # add 2 group members into MyGroup
  $groups->addGroupsTo(["Member2Group","Member3Group"],"MyGroup") ; 

  # add a single members into the previous member groups
  $groups->addSingleTo("single4","Member1Group") ;
  $groups->addSingleTo("single5","Member2Group") ;
  $groups->addSingleTo("single6","Member3Group") ;
  
  # flatten the group MyGroup
  @singles = $groups->getSinglesOf("MyGroup") ;  
  $present = $groups->isSingleOf("single4","MyGroup") ; 
  

DESCRIPTION

The Groups object implements a set of groups. Each group can own single members and group members. A group can be flattened, i.e. expansed until each of his members is a single one.

CONSTRUCTORS

new

Create a new group set.

  my $groups = new Set::Groups

INSTANCE METHODS

setDebug

Set a debug level.

  $groups->setDebug(1) ;
  

Set management

newGroup

Create a new empty group and add it into the set. A group is everything which can be a key of a hash. Returns 1 on success, 0 otherwise.

  $groups->newGroup("a_group") ;
  $groups->newGroup(1) ;

deleteGroup

Delete a group from the set. Return 1 on success, 0 otherwise.

  $groups->deleteGroup("a_group") ;
  

getGroups

Return the list of the groups present into the set.

  @groups = $groups->getGroups() ; 

getCyclicGroups

Return the list of the cyclic groups (i.e. self-contained) present into the set.

  @groups = $groups->getGroups() ; 

getAcyclicGroups

Return the list of the acyclic groups (i.e. not self-contained) present into the set.

  @groups = $groups->getGroups() ; 

hasGroup

Check if a group is present into the set.

  $present = $groups->hasGroup("a_group") ;

Groups management

addSingleTo

Add a single member to a group. A single is everything which can be a key of a hash. If the group doesn't exist in the set, it is created. Return 1 on success, 0 otherwise.

  $groups->addSingleTo("single","a_group") ;

addGroupTo

Add a group member to a group. If the embedding group doesn't exist in the set, it is created. If the member group doesn't exist in the set, it is created as an empty group. Return 1 on success, 0 otherwise.

  $groups->addGroupTo("group_member","a_group") ;
  

removeOwnSingleFrom

Remove an own single from a group. Return 1 on success, 0 otherwise.

  $groups->removeSingleFrom("single","a_group") ;

removeGroupFrom

Remove a group member from a group. Return 1 on success, 0 otherwise.

  $groups->removeGroupFrom("a_member_group","a_group") ;

isAcyclic

Check if a group is acyclic.

  $is_acyclic = $groups->isAcyclic("a_group") ;
  

isOwnSingleOf

Check if a single is an own member of a group.

  $present = $groups->isOwnSingleOf("single","a_group") ;

isGroupOf

Check if a group is member of a group.

  $present = $groups->isGroupOf("a_group_member","a_group") ;

isSingleOf

Check if a single is a (own or not) member of a group.

  $present = $groups->isSingleOf("single","an_acyclic_group") ;

Warning - Calling this method wich a cyclic group as argument gives a infinite recursion.

getOwnSinglesOf

Return the list of own singles of a group.

  @singles = $groups->getOwnSinglesOf("a_group") ;

getGroupsOf

Return the list of groups of a group.

  @groups = $groups->getGroupsOf("a_group") ;

getSinglesOf

Return the list of (own or not) singles of an acyclic group.

  @singles = $groups->getSinglesOf("an_acyclic_group") ;

Warning - Calling this method wich a cyclic group as argument gives a infinite recursion.

EXAMPLES

Suppose a group file like :

        admin:root,adm
        team:piotr,lioudmila,adam,annette,jacquelin
        true-users:james,sophie,@team,mohammed
        everybody:@admin,operator,@true-users
        daemon:apache,smmsp,named,daemon
        virtual:nobody,halt,@daemon
        all:@everybody,@virtual

where @name means group name, then the following code :

        use Set::Groups ;

        $groups = new Set::Groups ;
        while(<F>)
        {
          ($group,$members) = /^(\S+):(.*)$/ ;
          @members = split(/,/,$members) ;
          for $member (@members)
          {
            if ($member=~/^@/)
            {
              $member=~s/^@// ;
              $groups->addGroupTo($member,$group) ;
            }
            else
            {
              $groups->addSingleTo($member,$group) ;
            }
          }
        }
        die "some groups are cyclic" if scalar($groups->getCyclicGroups())>0 ;
        print join(', ',$groups->getSinglesOf("all")) ;

gives : apache, sophie, jacquelin, lioudmila, mohammed, smmsp, nobody, adm, annette, operator, james, named, adam, halt, root, daemon, piotr