【问题标题】:Undefined subroutine &main::promt未定义的子程序 &main::promt
【发布时间】:2015-11-02 16:06:32
【问题描述】:

我对 Perl 有点陌生。我正在尝试编写一个脚本,该脚本将采用 mysqldump 并将其恢复到新数据库中。主要思想是将数据库从一台服务器迁移到另一台服务器。

这是我写的脚本:

#!/usr/bin/perl

use warnings;

print "Starting the migration process!\n";

print "Enter the source server address, make sure you enter the FQDN of the server";
$source_address = promt ("Source server address: ");
check_string($source_address);
print "Enter the destination server address, make sure you enter the FQDN of the server";
$destination_address = promt ("Destination server address:");
check_string($destination_address);
print "Enter the Source server password for the root user";
$source_password = promt ("Source server address:");
check_string($source_password);
print "Enter the destination server password for the root user";
$destination_password = promt ("Destination server address:");
check_string($destination_password);

$current_dir = cwd();

system("mysqldump --single-transaction -u root -p$source_password --force -h $source_address -A -R -E --triggers
     --routines --max_allowed_packet=512M | gzip -c >$current_dir/old_db_dump.sql") or die "system call to create Mysqldump failed: $?";

system("pt-show-grants -uroot -p$source_password -h $source_address > $current_dir/old_grants.sql") or die "system call to create grant failed: $?";

system("mysql -u root -p$destination_password -h $destination_address < $current_dir/old_db_dump.sql") or die "System call to import the sqldump failed: $?";

system("mysql -u root -p$destination_password -h $destination_address < $current_dir/old_grants.sql") or die "System call to import the grants failed: $?";

# A function that checks if the passed string is null or not
sub check_string{
    $string_to_check = $_[0];
    if ($string_to_check eq '') {
    print "The entered value is empty, the program will exit now, re-run the program";
    exit 0;
    }
}

sub prompt {
    my ($text) = @_;
    print $text;

    my $answer = <STDIN>;
    chomp $answer;
    return $answer;
}

但是当我尝试执行代码时,我最终得到以下错误:

Starting the migration process!
Undefined subroutine &main::promt called at migrate_mysql.pl line 26.
Enter the source server address, make sure you enter the FQDN of the server

为了编写 Prompt 函数,我按照这里帖子中提到的教程进行操作:http://perlmaven.com/subroutines-and-functions-in-perl

我不知道为什么会出现此错误。我必须包含一些包吗?

如果你能对代码的系统块发表评论,那就太好了:

system("mysqldump --single-transaction -u root -p$source_password --force -h $source_address -A -R -E --triggers
     --routines --max_allowed_packet=512M | gzip -c >$current_dir/old_db_dump.sql") or die "system call to create Mysqldump failed: $?";

system("pt-show-grants -uroot -p$source_password -h $source_address > $current_dir/old_grants.sql") or die "system call to create grant failed: $?";

system("mysql -u root -p$destination_password -h $destination_address < $current_dir/old_db_dump.sql") or die "System call to import the sqldump failed: $?";

system("mysql -u root -p$destination_password -h $destination_address < $current_dir/old_grants.sql") or die "System call to import the grants failed: $?";

我的做法是否正确?我是否以正确的方式传递变量值?

【问题讨论】:

  • 错字:将sub promptpromt ("Source server address: "); 进行比较(您缺少p)。
  • 另外,除了use warnings;,你还应该use strict;

标签: perl system


【解决方案1】:

从此错误消息:

在 migrate_mysql.pl 第 26 行调用了未定义的子例程 &main::promt。

您应该查看第 26 行。这很奇怪,因为您的错误不在第 26 行,而是在这里:

$source_address = promt ("Source server address: ");

如果我运行你的代码,我会得到:

在第 9 行调用了未定义的子例程 &main::promt。

你有“prompt”而不是“prompt”,这是一个未定义的子例程。

你真的应该在你的代码中添加use strict;,然后重新调整它——它最初会产生更多的错误,但如果你错误地拼写一个变量,它将避免一些真正的陷阱。

在您的代码中非常简单 - 只需将 my 放在第一次使用变量的前面 - 否则您一直擅长范围界定,但除此之外,这意味着一旦您第一次使用 $string_to_check 它仍然可见程序的其余部分,这有点混乱,可能会导致一些奇怪的错误。

【讨论】:

    猜你喜欢
    • 2013-06-25
    • 2017-02-23
    • 1970-01-01
    • 2011-10-08
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2019-07-22
    相关资源
    最近更新 更多