I agree, this is probably a "faux" problem
If you use system to open a program, i.e:
`gimp`;
print "gimp was closed\n";
print "... lots and lots of commands next";
the script is stopped until the first line is finished, so the second, third etc lines, are "frozen" and never run until you close manually this gimp session.
delete a folder which is created by this program after it gets closed by the user
It depends on your specific case, you could need to add another system line below doing the cleanup, a rmdir $dir line, or nothing |