|
NAMEGeo::Postcodes::Selection - How to use the selection procedure/method of the Geo::Postcodes::* modules. INTRODUCTIONThis document uses the fictious country Utopia (with country code 'U2'), and the rest of this document will refer to the non-existent module "Geo::Postcodes::U2". SELECTIONselection proceduremy @postcodes = Geo::Postcodes::U2::selection($field => $value); This simple form will give a list of postcodes matching the specified field and value. Substitute 'U2' by a valid country subclass. The fields can be anyone given by the Geo::Postcodes::U2::get_fields() call, and the value either a literal text or a regular expression. The resulting list of postcodes is sorted. An empty list is also returned if one ore more of the arguments are invalid, but this can be checked by using the verify_selectionlist procedure described later on. It is possible to specify more than one field/value pair, but then the mode should be given (but it will default to 'and' otherwise). Use as many field/value-pairs as required. The mode can be specified initially, between the field/value pairs, or not at all. The following examples are equivalent: Geo::Postcodes::U2::selection('and', $field => $value,
$field2 => $value2);
Geo::Postcodes::U2::selection( $field => $value,
'and', $field2 => $value2);
Geo::Postcodes::U2::selection( $field => $value,
$field2 => $value2);
The field/value pairs are evaluated in the specified order, and the modes can be mixed freely: Geo::Postcodes::U2::selection($field1 => $value1,
'and', $field2 => $value2,
'or', $field3 => $value3,
'or not', $field3 => $value3,
'and not, $field4 => $value4,
'xor', $field5 => $value5);
Modes The modes (except 'all', 'none', 'not', and 'one') can be summarised by the following truth table: A B | and and nand nand nor nor or or xnor xnor xor xor
| not not not not not not
-----+----------------------------------------------------
0 0 | 0 0 1 1 1 0 0 1 1 0 0 1
0 1 | 0 0 1 1 0 1 1 0 0 1 1 0
1 0 | 0 1 1 0 0 0 1 1 0 1 1 0
1 1 | 1 0 0 1 0 0 1 1 1 0 0 1
Using 'not' after the mode negates the second argument, and the 'nxxx' variants negate the result.
The Search Value The search value is parsed as the regular expression "m{^$value$}i". This has the following implications:
Wildcards The character % (the percent character) will match zero or more arbitrary characters (and is borrowed from standard SQL). '%12' will match '1112' but not '1201'. 'O%D' will match all strings starting with an 'O' and ending with a 'D'. '%A%' will match all strings with an 'A' somewhere. (The character % is changed to the regular expression '.*' (dot star) by the module.) Regular expressions
External Procedures It is also possible to call external procedures, and have them decide if the postcode should be included. Specify "'procedure', \&procedure_to_do_the_job" instead of a field/value pair. The procedure is passed the postcode, and must return true or false. sub procedure_to_do_the_job
{
my postcode = shift;
return 1 if ...
return 0;
}
selection methodmy @postcodobjects = Geo::Postcodes::U2->selection(xxxx); This works just as the procedure version (see above), but will return a list of postcode objects (instead of just a list of postcodes). When not to use SelectionDo not use selection when you are after a single postcode, but use the constructor (the new call) or the xxx_of procedures to access the values of the fields directly. my @obj_list = Geo::Postcodes::U2->selection('postcode' -> '1178');
my $obj = Geo::Postcodes::U2->new('1178');
Using selection causes a loop that iterates over all the postcodes, and this is not a good idea when you have the answer already - a single postcode. The "valid" procedure can be used to check if the postcode actually exist. See the tutorial. SELECTION_LOOPIt is possible, even adviceable, to use external procedures in the argument list to the selection call itself, if the procedure is only used to select from the postcodes. The procedure shown in this section have a print statement, and is okay - if somewhat contrived. selection_loop procedureThe first argument is a pointer to a procedure which will be called for each postcode returned by the selection call, with the rest of the arguments. sub post_check
{
my $postcode = shift;
print "$postcode\n" if something($postcode) > 3.14;
}
Geo::Postcodes::U2::selection_loop(\&post_check, xxx, yyy);
selection_loop methodAs above, but the value passed to the specified procedure is a postcode object. sub post_check
{
my $object = shift;
print $object->postcode() . "\n" if something($object->postcode()) > 3.14;
}
Geo::Postcodes::U2->selection_loop(\&post_check, xxx, yyy);
SUPPORTING PROCEDURESverify_selectionlistThe selection procedure/method will return an empty list, if there are errors in the argument list, or if the selection did not find any matches. Use this procedure (from the child class) to verify that the arguments are valid for use by the selection procedure/method. my($status, @list) = Geo::Postcodes::U2::verify_selectionlist(@arguments); A status value of true (1) is followed by a modified version of the original arguments. This will replace things as 'and' 'not' by 'and not', but the selection procedure/method copes with both. (The returned list may be optimised in future versions of the module.) A status value of false (0) is followed by a list of diagnostic messages, up to the point where the verification failed. External procedures are recognised, and must actually exist for the test to accept them. Geo::Postcodes::is_legal_selectionmodeReturns true if the mode is one of the list returned by "get_selectionmodes", documented below. Geo::Postcodes::is_legal_initial_selectionmodeReturns true if the mode is one of the list returned by "get_initial_selectionmodes", documented below. Geo::Postcodes::get_selectionmodesA sorted list of legal selection modes; 'and', 'and not', 'nand', 'nand not', 'nor', 'nor not', 'or', 'or not', 'xnor', 'xnor not', 'xor' and 'xor not'. Geo::Postcodes::get_initial_selectionmodesAs Geo::Postcodes::get_selectionmode, with the addition of 'all', none', 'not' and 'one'. The list is sorted. SEE ALSOThe tutorial perldoc Geo::Postcodes::Tutorial or man Geo::Postcodes::Tutorial, and the documentation for the individual country modules; e.g. perldoc Geo::Postcodes::NO or man Geo::Postcodes::NO. COPYRIGHT AND LICENCECopyright (C) 2006 by Arne Sommer - perl@bbop.org This library is free software; you can redistribute them and/or modify it under the same terms as Perl itself. POD ERRORSHey! The above document had some coding errors, which are explained below:
|