- The module code
Regexgene - An example of a AI::Gene::Sequence
This is a short module which illustrates the way to use the AI::Gene::Sequence module.
use Regexgene; $regex = Regexgene->new(5); print $regex->regex, "\n"; $regex->mutate; print $regex->regex, "\n"; $copy = $regex->clone; $copy->mutate; print $regex->regex, "\n", $copy->regex, "\n";
The following is a code / pod mix, use the source. A programme using this module is available as
First we need to be nice, do our exporting and versions, we also need to tell perl that we want objects in this class to inherit from AI::Gene::Sequence by placing it in our @ISA array.
We have a load of globals, these form the basis of our token types, anything from the same array, is the same type eg.
@modifiers = qw( * *? + +? ?? ); # are of type 'm' for modifier
As we want to be able to fill our regular expression at the same time as we create it and because we will want to nest sequences we will need some way to know how deep we are, then a different new method is needed.
If called as an object method (
$obj-new>), this decreases the depth count by one from the invoking object. It also adds tokens to the regular expression and uses the valid_gene method to ensure we stay sane from the start.
As can be seen, we use array offsets above $self-> to store information which is specific to our implementation.
As we are going to allow nested sequences, then we need to make sure that when we copy an object we create new versions of everthing, rather than reusing pointers to data used by other objects.
This is where we really start needing to have our own implementation. This method is used by AI::Gene::Sequence when it needs a new token, we also use it ourselves when we create a new object, but we did not have to.
If we are provided with a token type, we use that, otherwise we chose one at random. We make sure that we return a two element list. If we had wanted, when passed a type of 'g' along with a second argument, we could have caused this method to mutate the nested regex, instead, we just create a different one.
Because we have restricted ourselves to simple regular expressions we only need to make sure that modifers and alternation do not follow or precede things they should not.
Note that we do not use the current version of the gene in $self->. This is because it is the un-mutated version, if we do not accept the mutation then $self will be left alone by our calling methods.
That said, if you want to use $self-> then you can, but it would be unwise to modify it here.
Having created a way to create, modify and verify our genetically encoded regular expressions, we could do with some way to actually use them. This method retuns a non compiled regular expression and calls itself recursively when it finds nested genes.
Alex Gough (firstname.lastname@example.org).
Copyright (c) 2001 Alex Gough <email@example.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.