Perl 未定义的子程序&;主要内容:promt
我对Perl有点陌生。我试图编写一个脚本,它将接受mysqldump并将其恢复到一个新的数据库中。其主要思想是将数据库从一台服务器迁移到另一台服务器 这是我写的纸条:Perl 未定义的子程序&;主要内容:promt,perl,system,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"; $sour
#!/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;
}
为了编写提示函数,我遵循了本文中提到的教程:
我不知道为什么我会在这里犯这个错误。我必须包括一些包裹吗
另外,如果您可以对代码的系统块进行注释也很好:
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: $?";
system(“mysqldump--single transaction-u root-p$source\u password--force-h$source\u address-A-R-E--触发器
--例程--max_allowed_packet=512M | gzip-c>$current_dir/old_db_dump.sql”)或die“创建Mysqldump的系统调用失败:$?”;
系统(“pt show grants-uroot-p$source\u password-h$source\u address>$current\u dir/old\u grants.sql”)或die“创建授权的系统调用失败:$?”;
系统(“mysql-u root-p$destination\u password-h$destination\u address<$current\u dir/old\u db\u dump.sql”)或die“导入sqldump的系统调用失败:$?”;
系统(“mysql-u root-p$destination\u password-h$destination\u address<$current\u dir/old\u grants.sql”)或die“导入授权的系统调用失败:$?”;
我这样做对吗?我是否以正确的方式传递变量值?来自此错误消息: 在migrate_mysql.pl第26行调用了未定义的子例程&main::promt 你应该看第26行。这很奇怪,因为您的错误不在第26行,但在这里:
$source_address = promt ("Source server address: ");
如果我运行您的代码,我会得到:
在第9行调用了未定义的子例程&main::promt
您得到的是“promt”而不是“prompt”,这是一个未定义的子例程
您还应该添加使用strict代码>到您的代码,然后重新调整它-最初它会生成更多的错误,但如果您拼写错误的变量,它将避免将来出现一些真正的错误
在你的代码中很容易——只要在第一次使用变量之前放上my
——你在其他方面的作用域很好,但在其他方面,这意味着一旦你第一次使用$string\u to\u check
,程序的其余部分仍然可以看到它,这只是有点混乱,可能会导致一些奇怪的错误 打字错误:将子提示符
与promt(“源服务器地址:”)进行比较代码>(您缺少一个p
)。此外,您应该使用严格的代码>除了使用警告之外代码>
$source_address = promt ("Source server address: ");