这里我试图使用perl cgi将变量从一个子例程传递到另一个子例程。但是我的变量没有从一个子例程传递到另一个子例程,
use strict;
use warnings;
use CGI ':standard';
print header;
print start_html("example");
print end_html;
my $file = "text.txt";
open my $fh,'<', $file or die $!;
sub1($fh);
sub2($fh);
sub sub1 {
my $fh = shift;
while ( my $line = <$fh> ) {
print $line;
}
return $fh;
}
sub sub2 {
my $line = shift;
print start_form;
print "<table>";
print "<th>content</th>";
print "<tr>";
print "<td>$line<td>";
print "</tr>";
print "</table>";
print end_form;
}
在上面的子程序中(即sub2 $行内容未从sub1传递)
答案 0 :(得分:0)
也许你想要
my $content = sub1($fh);
sub2($content);
sub sub1 {
my $fh = shift;
my $content;
while ( my $line = <$fh> ) {
print $line;
$content .= $line;
}
return $content;
}
文件句柄$ fh也可以在sub1调用后关闭