Perl 是否执行出现在 return 之后的代码?功能?
Does Perl execute code that appears after the return; function?
我有一个丑陋的 Perl 子例程,它有一个嵌套的 if 语句,returns 根据条件不同的值。这很丑陋,因为我不喜欢嵌套的 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";
}
不,Perl 不会在 return
之后执行代码。这就是 return
!
的重点
不要在 Perl 子程序中使用原型。只有 sub TacacsInstalled { ... }
是正确的
使用 snake_case
优先于 CamelCase
标识符
将所有变量声明为尽可能晚,永远不要在代码顶部的块中声明
正确使用Perl的语句修饰符
None 您的反引号字符串看起来是正确的:每个字符串的末尾都有一个备用双引号。双引号不需要在反引号字符串中转义
我还想说,返回 Perl true 或 false 值(0
、'0'
和undef
为假;其他均为真)优于返回 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';
}
我有一个丑陋的 Perl 子例程,它有一个嵌套的 if 语句,returns 根据条件不同的值。这很丑陋,因为我不喜欢嵌套的 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";
}
不,Perl 不会在 return
之后执行代码。这就是 return
!
不要在 Perl 子程序中使用原型。只有
sub TacacsInstalled { ... }
是正确的使用
snake_case
优先于CamelCase
标识符将所有变量声明为尽可能晚,永远不要在代码顶部的块中声明
正确使用Perl的语句修饰符
None 您的反引号字符串看起来是正确的:每个字符串的末尾都有一个备用双引号。双引号不需要在反引号字符串中转义
我还想说,返回 Perl true 或 false 值(0
、'0'
和undef
为假;其他均为真)优于返回 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';
}