我从html表单获取输入.有一堆文本输入,因此有一堆键值对.当一个人有超过三对时,你会看到我现在的方法非常乏味.那,或者我只是懒惰.
我想知道,是否有更有效的方法将哈希转换为一系列标量变量?我希望密钥是变量名,设置为密钥的值.
我对perl比较新,对不起,如果这是一个愚蠢的问题.
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI qw(:standard Vars); print "Content-type: text/html\n\n"; my %form = Vars(); $hourly = $form{hourly}; $hours_w = $form{hours_w}; $rent_m = $form{rent_m}; #...
解决方法
my $cgi; BEGIN { $cgi = CGI->new(); } BEGIN { # Only create variables we expect for security # and maintenance reasons. my @cgi_vars = qw( hourly hours_w rent_m ); for (@cgi_vars) { no strict 'refs'; ${$_} = $cgi->param($_); } # Declare the variables so they can be used # in the rest of the program with strict on. require vars; vars->import(map "\$$_",@cgi_vars); }