Before worrying about the amount of code, I'd first focus on making your code more idiomatic and self-documenting. Sometimes it takes more code, not less, to write good code. In addition to the indenting problem noted above, consider the following changes:

# your code looks like an extract from a larger document # but just in case you aren't # do use strictures and declare your variables # it will save you *A LOT* of debugging time use strict; use warnings; # DECLARE YOUR VARIABLES! my $FH = \*STDOUT; my $target_db = $ARGV[0]; my $target_os = $ARGV[1]; my $flag; while (my $line = <DATA>) { if ($line =~ /WNT/ || $line =~ /UNX/) { print "$line\n"; } # be consistant: # if you are unsetting a flag with undef, then always use # defined to check for an unset flag. Not everything that # is false is also undef. if (defined($flag)) { # Fix some potential bugs/cause for warnings: # * check to see if variables are defined before using # equals or regex comparisons. Otherwise you will get # warnings. # Make code more readable and self documenting # * negations are harder to read than positives # * conditions shouldn't be buried inside an else {...} if # they apply to the entire content of the block. # * or'd regexes can be combined into one to make it clearer # that one is interested in a set of alternatives if (defined($target_db)) { if ($target_db eq 'sql') { next if ($line =~ /informix|oracle/i); } elsif ($line =~ /$target_db/i) { next; } } #* conditions that apply to the entire block should be # part of the if/elsif clause UNLESS you expect to be # expanding a simple condition to a full fledged if-else print $FH "$line"; if (defined($target_os)) { # leaving room for a more complicated target_os logic # later on if ($line =~ /$target_os/) { undef $flag; next; } } } #* defined is a fast check, so do it first #* defined($flag) is better than (defined $flag) #* double quoting a single variable is redundant. #* if we're not expecting more complicated logic # combine all conditions that apply to the block into # the if statement if (!defined($flag) && ($line =~ /UNX|WNT|COMPONENT/)) { $flag = 1; print $FH $line; } }

Best, beth


In reply to Re: Looking for less code by ELISHEVA
in thread Looking for less code 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.