首页 > 解决方案 > perl 变量 perl 变量

问题描述

我的代码:

if ($funcarg =~ /^test (.*)/) {
  my $target1 = "http://$1/script/so.php";
    system("wget $target1");

所以,当我输入 perl a.pl 127.0.0.1 脚本必须下载http://127.0.0.1/script/so.php,但不幸的是它没有。我的错误在哪里?

 [root@localhost perl]# perl a.pl 127.0.0.1
http:///script/so.php: Invalid host name.

标签: perlvariables

解决方案


目前尚不清楚您的命令行参数如何从@ARGVto获取$funcarg。或者它是如何从127.0.0.1到(大概)test 127.0.0.1。我认为这就是你出错的地方。我认为这$funcarg不包含您在运行正则表达式匹配之前的想法。

这段代码做我认为你想要的。但我不得不编造两行(用注释标记),我很确定你对这两行的版本是你误解的地方。

#!/usr/bin/perl

use strict;
use warnings;

@ARGV or die "No argument given\n";

# I've made up the next two lines. But I think
# this is where your bug is.
my $host = $ARGV[0];
my $funcarg = "test $host";

if ($funcarg =~ /^test (.*)/) {
  my $target1 = "http://$1/script/so.php";
  print "$target1\n";
  system("wget $target1");
}

推荐阅读