我得到這些錯誤爲什麼我會在此Perl程序中收到有關未初始化值的警告?
Use of uninitialized value $vm_name in pattern match (m//) at testscript.pl line 164, <STDIN> line 1.
Use of uninitialized value $vm_name in concatenation (.) or string at testscript.pl line 173, <STDIN> line 1.
誰能幫助?我有一種感覺,這是與子程序,但不能完全弄清楚。我是Perl的新手,幾個小時一直在仔細研究這個問題。
# !/usr/bin/perl
use strict;
use warnings;
use feature ":5.10";
###############################################################################
# Static Variables
my $templateID = "XXXXXXXXXX";
my $serviceID = "XXXXXXXXXXX";
###############################################################################
###############################################################################
# Global Variables
my $zone;
my $input;
my $zone_ID;
my $vm_name;
my $net_default;
my $net_secondary;
my $vm_ID;
my $start;
my $char_count;
my $ipaddr_1;
my $ipaddr_2;
###############################################################################
# ----------------------- BASIC CONFIGURATIONS [ZONES] ------------------------
print " *** ";
print "
ZONES: paris, milan, berlin, geneva, amsterdam, london, slough";
print " which zone would you like to deploy to? \n ";
# Capture input for ZONE
$input = <STDIN>;
# Data formatting
$input =~ s/^\s+|\s+$|^\t//g;
$zone = lc $input;
# Declare ZONE to USER
print "You are deploying to $zone \n";
# Map input to function
# ZONE PARIS
if ($zone eq "paris") {
print "Deploying to $zone\n";
# assign zone id
$zone_ID = "374b937d-2051-4440-b02c-a314dd9cb27e";
}
# ZONE MILAN
elsif ($zone eq "milan") {
print "Deploying to $zone\n";
# assign zone id
$zone_ID = "58848a37-db49-4518-946a-88911db0ee2b";
}
# ZONE BERLIN
elsif ($zone eq "berlin") {
print "Deploying to $zone\n";
# assign zone id
$zone_ID = "fc129b38-d490-4cd9-acf8-838cf7eb168d";
}
# ZONE GENEVE
elsif ($zone eq "geneva") {
print "Deploying to $zone\n";
# assign zone id
$zone_ID = "1ef96ec0-9e51-4502-9a81-045bc37ecc0a";
}
# ZONE AMSTERDAM
elsif ($zone eq "amsterdam") {
print "Deploying to $zone\n";
# assign zone id
$zone_ID = "3c43b32b-fadf-4629-b8e9-61fb7a5b9bb8";
}
# ZONE SLOUGH
elsif ($zone eq "slough") {
print "Deploying to $zone\n";
# assign zone id
$zone_ID = "5343ddc2-919f-4d1b-a8e6-59f91d901f8e";
}
# ZONE LONDON
elsif ($zone eq "london") {
print "Deploying to $zone\n";
# assign zone id
$zone_ID = "f6b0d029-8e53-413b-99f3-e0a2a543ee1d";
}
# ELSE
else {
print "Please choose a ZONE";
system('./deploy_vm');
}
# SUBROUTINE: Assigning Virtual Machine name
sub vm_name {
# Name of Virtual Machine
print "Enter name of Virtual Machine: \n";
print "[VDC Bug: Please do not include '_' in the name]\n";
my $vm_name = <STDIN>;
# Data formatting
$vm_name =~ s/^\s+|\s+$//g;
print("VM Name: $vm_name");
}
# Apply input filters (no underscores, length constraints)
if (my $vm_name =~ /_/) {
return ('underscore error');
print "Cannot use underscore in VM name, please try again";
vm_name();
}
# DisPlay config
print "
$vm_name will be provisioned with:
1GB HDD and compute offering of 2CPUs with 2GB-RAM";
# ----------------------- NETWORK CONFIGURATIONS ------------------------------
# Gather network information
print "available networks in $zone";
system('cloudmonkey list networks zoneid=$zone_id filter=name,id');
# Users to choose Networks through display
print "";
# Default NIC & IP address
print "please enter Network ID for Default NIC:\n";
$net_default = <STDIN>;
# IP Address DEFAULT
print "please enter IP address\n";
$ipaddr_1 = <STDIN>;
# formatting IP address
$ipaddr_1 =~ m/^\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;
# SECONDARY NIC & IP address
print "please enter Network ID for secondary NIC";
$net_secondary = <STDIN>;
# IP Address SECONDARY
print "please enter IP address\n";
$ipaddr_2 = <STDIN>;
# Formatting IP address
$ipaddr_2 =~ m/^\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;
print "IP ADDRESS is $ipaddr_2\n";
# Deployment notice
print "... deploying ... \n";
# Provision Machine
# + startvm=false due to provisioning constraints
system('cloudmonkey deploy virtualmachine startvm=false name=$vm_name diplayname=$vm_name zoneid=$zone_id templateid=$templateID serviceofferingid=$serviceID networkids=$net_default ipaddress=$ipaddr_1');
# Add second network to VM
print "Adding second network to VM";
system('cloudmonkey addNicToVirtualMachine networkid=$network_id_second ipaddress=$ip_address_second virtualmachineid=$vm_id');
# Display machine ID
print "$vm_ID";
my $feedback = system('cloudmonkey list networks zoneid=$zone_id filter=name,id');
# ## ## ## ## ## ## ## ## ## ## ## ## ## ## #
# # Sotiris Comments # #
# ## ## ## ## ## ## ## ## ## ## ## ## ## ## #
# if (lc($feedback) =~ /error/) {
# Warn user and fall back to known state. }
# User to copy&paste ID
print "Paste VM ID:\n";
$vm_ID = <STDIN>;
# Data Validation
$vm_ID =~ s/^\s+|\s+$//g;
$vm_ID = lc $vm_ID;
$char_count = length($vm_ID);
# IF statement to impliment length validation
if ($char_count != 36) {
print "your ID is too long/short";
}
# Start Virtual Machine?
print "Start Virtual Machine $vm_name?";
$start = <STDIN>;
if ($start eq "yes") {
system('');
}
elsif ($start eq "no") {
system('');
}
# done!
您必須告訴我們哪一行是164和173. – TLP 2014-10-07 13:53:53
您的錯誤消息不與您的代碼對齊。 – 2014-10-07 13:55:09
我的歉意 第164行是直接在#Apply輸入過濾器(沒有下劃線,長度限制)標題下面開始的代碼,而第173行代碼是在#Dislay配置標題之後直接在 – 3c75 2014-10-07 14:11:53