未定义的子程序& main :: promt

时间:2015-11-02 16:06:32

标签: perl system

我是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

为了编写提示功能,我按照帖子中提到的教程进行了操作: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: $?";

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

1 个答案:

答案 0 :(得分:5)

从此错误消息:

  

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

你应该看第26行。这很奇怪,因为你的错误不在第26行,但在这里:

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

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

  

未定义的子程序&amp; main :: promt在第9行调用。

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

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

在你的代码中很简单 - 只需将my放在第一次使用变量之前 - 否则你已经很好地使用了scoping,但是否则它意味着你第一次使用$string_to_check程序的其余部分仍然可见,这有点乱,可能导致一些奇怪的错误。

相关问题