use strict; use warnings; use Group::Permutation; my $x = new Group::Permutation qw(1 3 2 4); print "x = $x\n"; print 'x of 2 = ',$x->of(2),"\n"; print 'ref of x of 2: ', ref $x->of(2),"\n"; print "x of 2 is three!\n" if ($x->of(2) == 3); #### x = (1, 3, 2, 4) x of 2 = 3 ref of x of 2: #### sub of { my $x = shift; my @y = map { $_-1 if defined && /^\d+$/ && $_>0 } @_; # protect the caller from placement errors return undef unless ($#y == $#_); return map { ($_<$$x{'n'}) ? $$x{'perm'}[$_] + 1 : $_+1 } @y; } #### sub new { # SNIP # BEGIN vicious error checking # all input must be legitimate $x{'perm'}=[ map { $_-1 if defined and /^\d+$/ and $_ > 0} @_ ]; return undef unless $#{$x{'perm'}} == $#_; # it must really be a permutation of 0..n-1 return undef unless join(' ',sort { $a <=> $b } @{$x{'perm'}}) eq join(' ',0..$#{$x{'perm'}}); # END error checking # SNIP $x{'n'} = scalar @{$x{'perm'}}; # for clarity in other places bless \%x, $class; }