-1

我有以下代码:

#!/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    <-- precedes the query parameter
http://www.example.com/index.php?route=9&other=HELLO     <-- replaces the query parameter
http://www.example.com/index.php?route=9&other=7HELLO    <-- succeeds the query parameter and so on for the rest of them....
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

我想要做什么

我试图获得与上面所示完全相同的foreach @insert_words输出(因此在 url 中的每个查询参数之前、替换和成功),但我想用更简单、更容易理解的方法替换复杂的正则表达式方法,但我不不知道最好的办法。

非常感谢您对此的帮助,非常感谢

4

1 回答 1

3

它在文档中描述了URI如何处理查询。该URI::QueryParam模块提供query_param允许与查询交互的子程序。

use strict;
use warnings;
use URI;
use URI::QueryParam;

my @words = qw(HELLO GOODBYE);
my $URL = <DATA>;
my $uri = URI->new($URL);

for my $key ($uri->query_param) {                    # the keys of the query
    my $org = $uri->query_param($key);               # keep original value
    for my $word (@words) {
        for ("$org$word", $word, "$word$org") {   
            $uri->query_param($key, $_);             # set new value
            print $uri->as_string, $/;               # print new uri
        }
    }
    $uri->query_param($key, $org);                   # restore original value
}

__DATA__
http://www.example.com/index.php?route=9&other=7

输出:

http://www.example.com/index.php?route=9HELLO&other=7
http://www.example.com/index.php?route=HELLO&other=7
http://www.example.com/index.php?route=HELLO9&other=7
http://www.example.com/index.php?route=9GOODBYE&other=7
http://www.example.com/index.php?route=GOODBYE&other=7
http://www.example.com/index.php?route=GOODBYE9&other=7
http://www.example.com/index.php?route=9&other=7HELLO
http://www.example.com/index.php?route=9&other=HELLO
http://www.example.com/index.php?route=9&other=HELLO7
http://www.example.com/index.php?route=9&other=7GOODBYE
http://www.example.com/index.php?route=9&other=GOODBYE
http://www.example.com/index.php?route=9&other=GOODBYE7
于 2013-03-12T14:34:11.607 回答