I know how to use m//g for search and s/// for substituting but I don't know how to to a "double search": after finding my match - finding another match inside.
I personally believe that -letting your actual problem aside for a moment- as soon as you know how to write a sub, you should have solved your problem: you should know that the /e modifier allows you to include code in the substitution part of an s///. So just capture the text you want to process, and process it in a separate sub. Of course, you can also inline the sub code in the substitution part itself. But then you will have to pay much care to the delimiters, or else the perl parser may easily get confused. For example, the whole program I suggested elsewhere may be cast into the form of a single substitution:
s/ ^ \b ( [ \w \s \[ \] ]+ \s+ \( ) $
( .*? ^\)$ )
/ my ($head, $body)=($1, $2);
$body =~ s|QUALIFIED||
unless $head ~~ m|^\w+?clk\[\d\]|;
$head . $body
/gemsx;