我有一个丑陋的Perl子例程,该子例程具有嵌套的if语句,该if语句根据条件返回不同的值。这很丑,因为我不喜欢嵌套if语句。它们很难遵循,因此我想将其分解为单独的if语句,但是我不确定它是否会改变子例程的执行方式。原始子例程如下:
sub TacacsInstalled(){
my $Installed;
my $InstalledVersion;
my $InstalledRelease;
my $NewVersion;
my $NewRelease;
$Installed = `rpm -qa | grep tac_plus | wc -l`;
chomp $Installed;
# Check to see if Tacacs is installed.
if ($Installed == 0){ # Tacacs is not installed. Fresh install.
return "False";
} else { # Tacacs is installed. Is it an old version?
$InstalledVersion = `rpm -q tac_plus --queryformat \"%{VERSION}\""`;
chomp $InstalledVersion;
$NewVersion = `rpm -qp ./tac_plus*.x86_64.rpm --queryformat \"%{VERSION}\""`;
chomp $NewVersion;
if ($InstalledVersion < $NewVersion) { # Current installed version is too old, must update.
return "False";
} else { # Maybe the release is newer.
$InstalledRelease = `rpm -q tac_plus --queryformat \"%{RELEASE}\""`;
chomp $InstalledRelease;
$NewRelease = `rpm -qp ./tac_plus*.x86_64.rpm --queryformat \"%{RELEASE}\""`;
chomp $NewRelease;
if ($InstalledRelease < $NewRelease) { # Current installed release is too old, must update.
return "False";
} else { # Installed release is newer than or equal to what we're trying to install. Do nothing.
return "True";
}
}
}
}
我可以改用以下代码来改进此代码吗?
sub TacacsInstalled(){
my $Installed;
my $InstalledVersion;
my $InstalledRelease;
my $NewVersion;
my $NewRelease;
$Installed = `rpm -qa | grep tac_plus | wc -l`;
chomp $Installed;
# Check to see if Tacacs is installed.
if ($Installed == 0){ return "False" }; # Tacacs is not installed perform a fresh install.
# If we got this far then Tacacs must be installed. Is it an old version?
$InstalledVersion = `rpm -q tac_plus --queryformat \"%{VERSION}\""`;
chomp $InstalledVersion;
$NewVersion = `rpm -qp ./tac_plus*.x86_64.rpm --queryformat \"%{VERSION}\""`;
chomp $NewVersion;
if ($InstalledVersion < $NewVersion) { return "False" }; # Current installed version is too old, must update.
# The Version is the same so then is the release is newer?
$InstalledRelease = `rpm -q tac_plus --queryformat \"%{RELEASE}\""`;
chomp $InstalledRelease;
$NewRelease = `rpm -qp ./tac_plus*.x86_64.rpm --queryformat \"%{RELEASE}\""`;
chomp $NewRelease;
if ($InstalledRelease < $NewRelease) { return "False" }; # Current installed release is too old, must update.
# Installed release is newer than or equal to what we're trying to install. Do nothing.
return "True";
}
答案 0 :(得分:2)
否,在return
之后,Perl不执行代码。这就是return
的重点!
答案 1 :(得分:1)
不要在Perl子例程上使用原型。只是sub TacacsInstalled { ... }
是正确的
使用snake_case
代替CamelCase
标识符
尽可能将所有变量声明为 late ,而不要在代码顶部的块中声明
正确使用Perl的语句修饰符
没有一个反引号字符串看起来正确:每个字符串的结尾都有一个多余的双引号。而且双引号不需要在反引号内转义
我还要说返回Perl true 或 false 值(0
,'0'
和undef
为false;一切都是真的)优于返回True
和False
字符串
我会写这个
sub tacacs_installed {
my $installed = `rpm -qa | grep tac_plus | wc -l`;
chomp $installed;
return 'False' if $installed == 0; # Tacacs is not installed. Fresh install.
my $installed_version = `rpm -q tac_plus --queryformat \"%{VERSION}\""`;
chomp $installed_version;
my $new_version = `rpm -qp ./tac_plus*.x86_64.rpm --queryformat \"%{VERSION}\""`;
chomp $new_version;
return 'False' if $installed_version < $new_version; # Current installed version is too old, must update.
my $installed_release = `rpm -q tac_plus --queryformat \"%{RELEASE}\""`;
chomp $installed_release;
my $new_release = `rpm -qp ./tac_plus*.x86_64.rpm --queryformat \"%{RELEASE}\""`;
chomp $new_release;
return 'False' if $installed_release < $new_release; # Current installed release is too old, must update.
return 'True';
}