简体   繁体   English

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

[英]Undefined subroutine &main::promt

I am sort of new to Perl.我对 Perl 有点陌生。 I was trying to write a script which will take a mysqldump and restore it in a new database.我正在尝试编写一个脚本,该脚本将采用 mysqldump 并将其恢复到新数据库中。 The main idea is to migrate a DB from one server to another.主要思想是将数据库从一台服务器迁移到另一台服务器。

Here's the scrip that I wrote:这是我写的脚本:

#!/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;
}

But when I try to execute the code, I end up with the following error:但是当我尝试执行代码时,最终出现以下错误:

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

For writing the Prompt function, I followed the tutorial mentioned in the post here: http://perlmaven.com/subroutines-and-functions-in-perl为了编写 Prompt 函数,我遵循了此处帖子中提到的教程: http : //perlmaven.com/subroutines-and-functions-in-perl

I do not know why am I getting this error here.我不知道为什么我在这里收到这个错误。 Do I have to include some packages?我必须包含一些包吗?

Also it would be nice if you could comment on the system block of the code:如果您可以对代码的系统块发表评论,那就太好了:

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: $?";

Am I doing it in the right way?我是否以正确的方式做这件事? Am I passing the variable values in a correct manner?我是否以正确的方式传递变量值?

From this error message:从这个错误信息:

Undefined subroutine &main::promt called at migrate_mysql.pl line 26.在 migrate_mysql.pl 第 26 行调用了未定义的子程序 &main::promt。

You should look at line 26. Which is odd, because your error isn't on line 26, but here:您应该查看第 26 行。这很奇怪,因为您的错误不在第 26 行,而是在这里:

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

If I run your code I get:如果我运行你的代码,我会得到:

Undefined subroutine &main::promt called at line 9.未定义的子程序 &main::promt 在第 9 行调用。

You've got "promt" not "prompt" which is a subroutine that is undefined.你有“promt”而不是“prompt”,这是一个未定义的子程序。

You should really also add use strict;你真的还应该添加use strict; to your code, and then rejig it - it'll generate a lot more errors initially, but it'll avoid some real gotchas in future if you spell a variable incorrectly.到你的代码,然后重新调整它 - 它最初会产生更多的错误,但如果你拼写错误的变量,它会在未来避免一些真正的问题。

It's quite easy in your code - just put my in front of the first use of a variable - you've been good with scoping otherwise, but otherwise it means that once you first use $string_to_check it remains visible to the rest of the program, which is just a bit messy and can lead to some odd bugs.在你的代码中很容易 - 只需将my放在第一次使用变量的前面 - 否则你已经擅长范围,但否则意味着一旦你第一次使用$string_to_check它仍然对程序的其余部分可见,这只是有点乱,可能会导致一些奇怪的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM