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.


In reply to Re: Looking for logically smart solution by ELISHEVA
in thread Looking for logically smart solution by Sun751

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.