我有这个代码:
#!/usr/bin/perl
package Modules::TextStuff;
use strict;
use warnings;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(get_text);
my $author;
my $text_tmp1 =<<'ENG';
This is a template text
by $author.
ENG
sub get_text {
my $tmp = shift @_;
$author = shift @_;
print "In sub author= $author lang = $tmp \n";
my $final_str = eval('$text_'.$tmp);
print "$final_str \n";
return $final_str;
}
1;
测试脚本:
#!/usr/bin/perl
use strict;
use warnings;
use Modules::TextStuff;
my $str = get_text('tmp1','jim');
print $str;
当我运行测试脚本时它不起作用。我得到:
在子作者=jim lang = eng
变量“$text_tmp1”在 (eval 1) 第 2 行不可用。在连接 (.) 或字符串中使用未初始化的值 $final_str
我怎样才能解决这个问题?