我可以用Perl随机化参数吗?

时间:2012-09-27 09:44:31

标签: perl parameters

有一种方法可以让Perl随机化所有参数,例如:

http://s.example.com/x?list=hahaha&a=&b=&c=&1

因此所有参数都带有& a =& b =& c =& 1 etc.etc.etc。

这是我期望的输出?

for (1..10){

#random this http://s.example.com/x?list=hahaha&a=&b=&c=&1
#to be like this


print "\n";
#http://s.example.com/x?list=hahaha&b=&a=&c=&1
#http://s.example.com/x?list=hahaha&c=&a=&b=&1
##http://s.example.com/x?list=hahaha&1=&b=&1=&c

}

所以有人可以帮忙解决这个问题吗?

Thnx已经

1 个答案:

答案 0 :(得分:1)

您提供的信息并不多,但这是一种(非常简单的)方法,可以帮助您入门。

代码

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';
use URI;
use URI::QueryParam;

# prepare
my @all  = map {chr $_} 32 .. 126;
my @word = grep /\w/ => @all;

# randomization helpers
sub rand_number      { 1 + rand 10 }
sub rand_param_name  { join '' => map {$word[rand @word]} 1 .. rand_number }
sub rand_param_value { join '' => map {$all[rand @all]}   1 .. rand_number }

# create three uris with randomized parameters
for (1 .. 3) {

    # prepare
    my $uri = URI->new('http://s.example.com/x');

    # add randomized parameters
    $uri->query_param(rand_param_name() => rand_param_value)
        for 1 .. rand_number;

    # done
    say $uri;
}

输出

http://s.example.com/x?TC10=MX3+B_i!
http://s.example.com/x?BaXgf=%26%7BY%5Dxt%25o&l0f=HM%5Cd%24JB%3E)&lDQ7a94=Xh%3AB3N1jK_&dzGNz3=Y8NX4o9%5BS&YnCMxDdTew=J%5B%7B~b%23&Mr8UpMhPo=H&8_nAwh8ZHe=9%5DRY%40V&EgheAy='Uk&gmqTwKW=7PyitIn&3=V%5EKApdS%2C
http://s.example.com/x?dBA7WswJ5=GX-x&yKHeb=1y1sq%3EjPt&7G=7oa%24XF%7C)jQ&FjVo5IpF=%3BZ%5C&JB7OLer=%7C55&bYo64Fiy=L%23%40&ZhSohJVZ=-W+Rp5c+%5DD&aP=zJP%7Bu&pE5URXCWq=PHZJ%60%3D97L&6Xw7Da7=.Drb