Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
perl:函数调用_Perl_Function - Fatal编程技术网

perl:函数调用

perl:函数调用,perl,function,Perl,Function,下面的脚本用于ssh到路由器,从abc.txt文件读取信息并执行命令。脚本按预期工作 use strict; use warnings; use autodie; use feature qw/say/; use Net::SSH::Expect; print "\n[INFO] script Execution Started\n"; my $ssh = Net::SSH::Expect->new( host => "ip addr", password =&

下面的脚本用于ssh到路由器,从abc.txt文件读取信息并执行命令。脚本按预期工作

use strict;
use warnings;
use autodie;
use feature qw/say/;
use Net::SSH::Expect;

print "\n[INFO] script Execution Started\n";


my $ssh = Net::SSH::Expect->new(
  host     => "ip addr",
  password => ' user ',
  user     => 'pwd',
  raw_pty  => 1,
);

my $login_output = $ssh->login();


$ssh->exec("enter command 1");
$ssh->exec("enter command 2");
open my $pr, '<', 'abc.txt';
while (my $config = <$pr>) {
chomp $config;
my $conf =  $ssh->exec("$config");
print("$conf");

}

函数调用部分不工作,不会抛出任何错误。我在这里遗漏了什么吗?

如果使用正确的缩进,您的错误就会变得明显:

sub mysub {
    my ($ssh,$filename) = @_;
    $ssh->exec("command 1");
    $ssh->exec("command 2");
    open my $pr, '<', $filename;
    while (my $config = <$pr>)
    {
        chomp $config;
        my $conf =  $ssh->exec("$config");
        print("$conf");

    }
    mysub($ssh,"abc.txt");   # this should be outside
}
sub-mysub{
我的($ssh,$filename)=@;
$ssh->exec(“命令1”);
$ssh->exec(“命令2”);
打开我的$pr,'
sub mysub {
    my ($ssh,$filename) = @_;
    $ssh->exec("command 1");
    $ssh->exec("command 2");
    open my $pr, '<', $filename;
    while (my $config = <$pr>)
    {
        chomp $config;
        my $conf =  $ssh->exec("$config");
        print("$conf");

    }
    mysub($ssh,"abc.txt");   # this should be outside
}