In addition to toolic's suggestions above, you could make the following two lines more compact by using the "e" (evaluate) modifier in your regular expression and a prefix "++". Prefix ++ adds one before using the variable value (postfix ++ adds it after using the variable value).
$ver = $ver +1;
$command =~ s/<PATCH NUMBER>/$ver/;
#can be rewritten as:
$command =~ s/<PATCH NUMBER>/++$ver/e;
However ... your title says that you want the logically smart solution. Logically smart and shorter aren't necessarily the same thing.
If, for example, a line can only have "<VER NUMBER>" or "<VER NUMBER + 1>" but not both, you would be better off using "if..elsif" rather than repeated ifs. Although it would be more characters (3 to be exact), you would eliminate an unnecessary processing step since Perl can ignore the second if whenever the first matches:
if ($command =~ /<VER NUMBER> /)
{
$command =~ s/<VER NUMBER>/$ver/;
}
elsif ($command =~ /<VER NUMBER + 1> /)
{
$ver = $ver +1;
$command =~ s/<PATCH NUMBER>/$ver/;
}
Alternatively, if substitution is the only behavior dependent on matching /<VER NUMBER> /, then toolic's suggestion of collapsing the first if ($command =~ /<VER NUMBER> / statement and the substitution makes logical sense. However, if you will soon have other steps that apply only when /<VER NUMBER> / matches, then still might want to keep the if statement, but replace it with the longer if ($command =~ s/<VER NUMBER> /$ver/) { ...my other steps...}. Substitution commands return the number of substitutions made so if there is no match, s/// will return 0.
Matching on a specific number and kind of whitespace isn't usually reliable unless your input is computer generated. People are notoriously bad at judging how many spaces they have typed or even whether the whitespace contains actual spaces or tabs. Additionally, "+" is a special character in regular expressions, so /<VER NUMBER + 1>/ matches "<VER NUMBER,followed by two or more spaces, followed by "1>". It does not match the literal string "<VER NUMBER + 1>".
You can match an unlimited amount of whitespace using \s+. You can match a literal "+" using \+.
Most likely, you would be better off changing your regular expressions to /<VER\s+NUMBER>\s/, /<VER\s+NUMBER>\s+\+\s+1>\s, and /<PATCH\s+NUMBER>. If so, you will have increased the number of characters by 13 (or 16 if we include "elsif"):
if ($command =~ /<VER\s+NUMBER>\s/)
{
$command =~ s/<VER\s+NUMBER>/$ver/;
}
elsif ($command =~ /<VER\s+NUMBER\s+\+\s+1>\s/)
{
$ver = $ver +1;
$command =~ s/<PATCH\s+NUMBER>/$ver/;
}
But have you noticed how much I am hedging: "if, for example", "Most likely", etc? In truth, if your goal is better code logic, you will need to tell us more about what your inputs look like and what you are trying to do. Without that, the best we can offer you are fairly superficial changes in "spelling", e.g. toolic's suggestion that you change $ver = $ver + 1 to $ver++ or my suggestion above to use the "e" modifier in your regular expression. Changes like that will make your code more "Perlish", but I don't know that they will help you be a truly smart coder.
Best, beth
Update:Added suggestion to use "e" modifier. |