我有以下代码:
#!/usr/bin/perl
use strict;
use warnings;
#use diagnostics;
use URI qw( );
my @insert_words = qw(HELLO GOODBYE);
while (<DATA>) {
chomp;
my $url = URI->new($_);
my $query = $url->query;
foreach (@insert_words) {
# Use package vars to communicate with /(?{})/ blocks.
local our $insert_word = $_;
local our @queries;
if (defined $query) {
$query =~ m{
^(.*[/=])([^/=&]*)((?:[/=&].*)?)\z
(?{
if (length $2) {
push @queries, "$1$insert_word$2$3";
push @queries, "$1$insert_word$3";
push @queries, "$1$2$insert_word$3";
}
})
(?!)
}x;
}
if (@queries) {
for (@queries) {
$url->query($_);
print $url, "\n";
}
}
else {
print $url, "\n";
}
}
}
__DATA__
http://www.example.com/index.php?route=9&other=7
上面的代码可以正常工作并产生以下输出:
http://www.example.com/index.php?route=9&other=HELLO7
http://www.example.com/index.php?route=9&other=HELLO
http://www.example.com/index.php?route=9&other=7HELLO
http://www.example.com/index.php?route=HELLO9&other=7
http://www.example.com/index.php?route=HELLO&other=7
http://www.example.com/index.php?route=9HELLO&other=7
http://www.example.com/index.php?route=9&other=GOODBYE7
http://www.example.com/index.php?route=9&other=GOODBYE
http://www.example.com/index.php?route=9&other=7GOODBYE
http://www.example.com/index.php?route=GOODBYE9&other=7
http://www.example.com/index.php?route=GOODBYE&other=7
http://www.example.com/index.php?route=9GOODBYE&other=7
如您所见,它将数组中的单词插入到 url 的特定位置。
我现在遇到的问题:
我现在想添加功能来执行HELLO
and GOODBYE
(或 中的任何内容@insert_words
)的所有可能组合,例如,它还应该将以下 url 添加到我已经得到的输出中:
http://www.example.com/index.php?route=HELLO&other=GOODBYE
http://www.example.com/index.php?route=HELLO&other=HELLO
http://www.example.com/index.php?route=GOODBYE&other=HELLO
http://www.example.com/index.php?route=GOODBYE&other=GOODBYE
但我不知道如何以最好的方式解决这个问题?
非常感谢您对此的帮助,非常感谢