| 1 | # ------ Provide anchored SQL wildcards ('^', '$') | ||||||||||||||||||
| 2 | package SQL::AnchoredWildcards; | ||||||||||||||||||
| 3 | |||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | # ------ use/require pragmas | ||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 1 | require Exporter; | |||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | # ------ define variables | ||||||||||||||||||
| 14 | 1 | @ISA = qw(Exporter); # we are an Exporter | |||||||||||||||||
| 15 | 1 | @EXPORT = # we export by default: | |||||||||||||||||
| 16 | qw(sql_anchor_wildcards); # a function for anchoring SQL wildcards | ||||||||||||||||||
| 17 | 1 | @EXPORT_OK = (); # we export nothing upon request | |||||||||||||||||
| 18 | 1 | $VERSION = 1.0; | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | # Preloaded methods go here. | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | # ------ enhance SQL wildcard processing by simulating '^' and '$' | ||||||||||||||||||
| 27 | 19 | ▶ | sub sql_anchor_wildcards { | ||||||||||||||||
|
Subroutine
sql_anchor_wildcards: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||
| 28 | 19 | local $_ = shift; # copy of unenhanced searchtext | |||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | # ------ escape SQL '%' and convert to lower case | ||||||||||||||||||
| 32 | 19 | s/%/\%/g; | |||||||||||||||||
| 33 | 19 | tr/A-Z/a-z/; | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | # ------ search unanchored at start by user's choice | ||||||||||||||||||
| 37 | 19 | ▶ | if (m/^%/o) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 38 | ; # do nothing | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | # ------ search anchored at start | ||||||||||||||||||
| 42 | } elsif (m/^\^/o) { | ||||||||||||||||||
| 43 | 9 | s/^\^//o; # convert to anchored at start SQL | |||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | # ------ search not anchored at start, but begins with escaped '^' | ||||||||||||||||||
| 47 | } elsif (m/^\\\^/o) { | ||||||||||||||||||
| 48 | 4 | s/^\\//o; # allow escaped '^' | |||||||||||||||||
| 49 | 4 | $_ = "%" . $_; # convert to unanchored SQL search | |||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | # ------ search not anchored at start | ||||||||||||||||||
| 53 | } else { | ||||||||||||||||||
| 54 | 3 | $_ = "%" . $_; # converted to unanchored SQL search | |||||||||||||||||
| 55 | } | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | # ------ search unanchored at end | ||||||||||||||||||
| 59 | 19 | ▶ | if (m/%$/o) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 60 | ; # do nothing | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | # ------ search not anchored at end, but ends with escaped '$' | ||||||||||||||||||
| 64 | } elsif (m/\\\$$/o) { | ||||||||||||||||||
| 65 | 4 | s/\\\$$/\$/o; # allow escaped '$' | |||||||||||||||||
| 66 | 4 | $_ .= "%"; # convert to unanchored SQL search | |||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | # ------ search anchored at end | ||||||||||||||||||
| 70 | } elsif (m/\$$/o) { | ||||||||||||||||||
| 71 | 9 | s/\$$//o; # convert to anchored at end SQL search | |||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | # ------ search not anchored at end | ||||||||||||||||||
| 75 | } else { | ||||||||||||||||||
| 76 | 3 | $_ .= "%"; # convert to unanchored SQL search | |||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | # return converted searchtext | ||||||||||||||||||
| 80 | 19 | return $_; | |||||||||||||||||
| 81 | } | ||||||||||||||||||
| 82 | 1 | 1; | |||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 1 | 1; | |||||||||||||||||
| 89 | __END__ | ||||||||||||||||||