in reply to Looking for less code

It seems that all of this fiddling with the $flag could be replaced by a flip-flop operator:
while (<DATA>) { ... if (/UNX|WNT|COMPONENT/ .. /$target_os/) { ... } }

See perlop for details on the flip-flop operator.

But without having a textual description of what your program does it seems hard for me to check if that does what you want.