简体   繁体   中英

Undefined subroutine &main::promt

I am sort of new to Perl. I was trying to write a script which will take a mysqldump and restore it in a new database. 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

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.

You should look at line 26. Which is odd, because your error isn't on line 26, but here:

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

If I run your code I get:

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

You've got "promt" not "prompt" which is a subroutine that is undefined.

You should really also add 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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