# pragmata ######################################################### use warnings; use strict; # modules ########################################################## use Test::More # tests => ?? + 1 # Test::NoWarnings adds 1 test 'no_plan' ; use Test::NoWarnings; use Data::Dump qw(dd); use constant DEBUG => 0; # prototypes ####################################################### sub prepare_excluded ($$); # prototyping might actually be useful here # main program ##################################################### my $sep = qr{ - }xms; # what's a word? this is a very naive definition (includes digits!). my $word = qr{ (? [ # followed by freely mixed list of words and patterns to exclude # raw words 'web', # regex objects mixed in freely qr{ (? [ qw(program call) ]); DEBUG and print qq{post: $exclude_post_word \n\n}; VECTOR: for my $ar_vector ( [ 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, 23-x speed.', 'WeB-developer, perl-pRoGrAm, element-explicit, function-CaLl, 23-x speed.', ], [ 'developer-Web, program-perl, explicit-element, call-function, x-23 speed.', 'Web-developer, perl-program, element-explicit, function-call, 23-x speed.', ], ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; (my $got = $string) =~ s{ ($exclude_pre_word) ($sep) ($exclude_post_word) } {$3$2$1}xmsg; is $got, $expected, qq{'$string'}; } # for VECTOR note "null pre- and post-exclusion list (everything swapped)"; $exclude_pre_word = prepare_excluded($word => []); $exclude_post_word = prepare_excluded($word => []); VECTOR: for my $ar_vector ( [ 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, 23-x speed.', 'developer-WeB, pRoGrAm-perl, element-explicit, CaLl-function, x-23 speed.', ], [ 'developer-Web, program-perl, explicit-element, call-function, x-23 speed.', 'Web-developer, perl-program, element-explicit, function-call, 23-x speed.', ], ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; (my $got = $string) =~ s{ ($exclude_pre_word) ($sep) ($exclude_post_word) } {$3$2$1}xmsg; is $got, $expected, qq{'$string'}; } # for VECTOR note "universal pre- and post-exclusion list (nothing swapped)"; $exclude_pre_word = prepare_excluded($word => [ $word ]); $exclude_post_word = prepare_excluded($word => [ $word ]); VECTOR: for my $ar_vector ( [ 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, 23-x speed.', 'WeB-developer, perl-pRoGrAm, explicit-element, function-CaLl, 23-x speed.', ], [ 'developer-Web, program-perl, explicit-element, call-function, x-23 speed.', 'developer-Web, program-perl, explicit-element, call-function, x-23 speed.', ], ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; (my $got = $string) =~ s{ ($exclude_pre_word) ($sep) ($exclude_post_word) } {$3$2$1}xmsg; is $got, $expected, qq{'$string'}; } # for VECTOR done_testing; # subroutines ###################################################### sub prepare_excluded ($$) { # prototyping might actually be useful here my ($word, # generic acceptable word pattern $ar_exclusions, # array ref.: words/patterns to exclude ) = @_; # avoid (?!) case which always fails. return qr{ $word }xms unless @$ar_exclusions; my ($exclude) = map qr{ $_ }xms, join ' | ', map prepare_single_exclusion($_), @$ar_exclusions ; return qr{ (?! $exclude) $word }xms; } sub prepare_single_exclusion { my ($this, # regex pattern or word-like thing ) = @_; # already a regex object: just return it. return $this if 'Regexp' eq ref $this; # pure alpha: return guarded alpha sequence. return qr{ (?i) (?