【发布时间】:2018-07-12 12:26:16
【问题描述】:
我正在开发一个在不同文件中使用不同子例程的程序。
分为三个部分
带有子程序名称的文本文件
带有子例程的 Perl 程序
提取子程序名称并启动它的主程序
子程序从文本文件中获取数据。
我需要用户选择文本文件,然后程序提取子程序的名称。
文本文件包含
cycle.name=cycle01
这是主程序:
# !/usr/bin/perl -w
use strict;
use warnings;
use cycle01;
my $nb_cycle = 10;
# The user chooses a text file
print STDERR "\nfilename: ";
chomp($filename = <STDIN>);
# Extract the name of the cycle
open (my $fh, "<", "$filename.txt") or die "cannot open $filename";
while ( <$fh> ) {
if ( /cycle\.name/ ) {
(undef, $cycleToUse) = split /\s*=\s*/;
}
}
# I then try to launch the subroutine by passing variables.
# This fails because the subroutine is defined as a variable.
$cycleToUse($filename, $nb_cycle);
这是另一个文件中的子程序
# !/usr/bin/perl
package cycle01;
use strict;
use warnings;
sub cycle01 {
# Get the total number of arguments passed
my ($filename, $nb_cycle) = @_;
print "$filename, $nb_cycle";
【问题讨论】:
标签: perl subroutine data-transfer