2015-11-02 74 views
-2

我對Perl很陌生。我正在嘗試編寫一個腳本,它將獲取一個mysqldump並將其恢復到一個新的數據庫中。主要想法是將數據庫從一臺服務器遷移到另一臺服務器。未定義的子程序和main :: promt

下面是我寫的紙條:

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

上午我以正確的方式做這件事?我是否以正確的方式傳遞變量值?

+5

錯字:比較'子提示符'到'promt(「源服務器地址:」);'(你錯過了'p')。 – ThisSuitIsBlackNot

+5

另外,除了使用警告外,還應該使用strict;'' – ThisSuitIsBlackNot

回答

5

從這個錯誤消息:

Undefined subroutine &main::promt called at migrate_mysql.pl line 26.

你應該看看線26這是奇怪的,因爲你的錯誤不就行了26,但在這裏:

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

如果我運行你的代碼我得到:

Undefined subroutine &main::promt called at line 9.

你有「promt」不是「提示」這是一個未定義的子程序。

你真的應該也加入use strict;到你的代碼中,然後重新調整它 - 它會在最初產生更多的錯誤,但是如果你錯誤地拼寫了一個變量,將會避免一些真正的陷阱。

它在你的代碼很簡單 - 只要把my中率先採用可變面前 - 你已經很好用作用域否則,但除此之外,它意味着一旦你第一次使用$string_to_check它仍然可見的休息該程序只是有點麻煩,可能會導致一些奇怪的錯誤。

相關問題