1

sample.pl の eval 関数に引数を渡し、別の Perl ファイル sample1.pl で関数を呼び出すための次のコードを作成しました。

sample1.pl:

use strict;
use warnings;
require 'sample.pl';
use subs "hello";
my $main2 = hello();
sub hello
 {
   print "Hello World!\n";
    our $a=10;
    our $b=20;
    my $str="sample.pl";
    my $xc=eval "sub{$str($a,$b)}";
 }

サンプル.pl

use strict;
use warnings;
our $a;
our $b;
use subs "hello_world";
my $sdf=hello_world();
sub hello_world($a,$b)
 { 
    print "Hello World!\n";
    our $c=$a+$b;   
    print "This is the called function from sample !\n";
    print "C: " .$c;

 } 1;

次のような出力が得られます。

Illegal character in prototype for main::hello_world : $a,$b at D:/workspace/SamplePerl_project/sample.pl line 6.
Use of uninitialized value $b in addition (+) at D:/workspace/SamplePerl_project/sample.pl line 9.
Use of uninitialized value $a in addition (+) at D:/workspace/SamplePerl_project/sample.pl line 9.
Hello World!
This is the called function from sample !
C: 0Hello World!

引数を渡して eval を介して関数を呼び出す方法を教えてください。

4

2 に答える 2

0

これを試してください これはあなたを助けるでしょう..

         my $action="your function name";
         if ($action) {
             eval "&$action($a,$b)";
         }

受信機能で

   sub your function name {
      my ($a,$b) =@_;#these are the arguments 
   }
于 2013-10-30T10:50:36.077 に答える