perl eval的问题

0 perl eval

我有以下代码,它将$mystring通过从文件中读取来设置值.

#!/usr/bin/perl

use warnings;
use strict;

use Data::Dumper;

my $mystring1 = "vault.\$_[0].org/\$_[1].\$_[2]/os/\$_[4]";
my $mystring2 = "vault.$_[0].org/$_[1].$_[2]/os/$_[4]";

&myfunc ("A1", "b2", "C3", "d4");

sub myfunc {

    print Dumper(\@_);

    print ("Before=$mystring1\n");
    my $test = eval($mystring1);
    print ("test=$test\n");

    print ("Before=$mystring2\n");
    $test = eval($mystring2);
    print ("test=$test\n");
}
Run Code Online (Sandbox Code Playgroud)

但是会生成以下输出:

Use of uninitialized value $_[0] in concatenation (.) or string at ./t.pl line 7.  
Use of uninitialized value in concatenation (.) or string at ./t.pl line 7.  
Use of uninitialized value in concatenation (.) or string at ./t.pl line 7.  
Use of uninitialized value in concatenation (.) or string at ./t.pl line 7.  
$VAR1 = [  
          'A1',  
          'b2',  
          'C3',  
          'd4'  
        ];  
Before=vault.$_[0].org/$_[1].$_[2]/os/$_[4]  
Use of uninitialized value $test in concatenation (.) or string at ./t.pl line 15.  
test=  
Before=vault..org/./os/  
Use of uninitialized value $test in concatenation (.) or string at ./t.pl line 18.  
test=  
Run Code Online (Sandbox Code Playgroud)

造成这些问题的原因是什么?如何实现以下目标?

test=vault.A1.org/b2.C3/os/d4
Run Code Online (Sandbox Code Playgroud)

我将如何使用eval或创建子程序来使其工作?

Hay*_*den 5

我不会用eval.一般来说,除非绝对必要,否则应该避免由于与之相关的安全隐患.在你的情况下,正如你所说,这可能是一个坏主意

mystring的值在读入的文件中设置

这意味着你的eval电话可以执行任意用户输入...一个主意!

为什么不使用sprintf

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $mystring1="vault.%s.org/%s.%s/os/%s";

myfunc ("A1", "b2", "C3", "d4");

sub myfunc {
    print Dumper(\@_);
    my $test = sprintf $mystring1, @_;
    print ("test=$test\n");
}
Run Code Online (Sandbox Code Playgroud)

一个简单的解决方案,可以满足您的一切