【发布时间】:2015-09-27 11:07:48
【问题描述】:
我正在编写一个 perl 脚本,我想将输出文件的文件名传递给子例程。
我尝试过这样的事情:
use strict;
use warnings;
test("Output.dat");
sub test {
my $name = @_;
open(B, ">$name") or die "Failure \n";
print B "This is a test! \n";
close(B);
}
我要多次使用子程序,所以我必须传递文件名并且不能在子程序中声明它。
我希望你能帮助我:)
【问题讨论】:
-
您必须在列表上下文中分配输入变量(注意括号)
my ($name) = @_;还要检查perlmaven.com/scalar-and-list-context-in-perl
标签: perl file io subroutine