in reply to perl code to replace
You don't show us any code, so I won't show any code either:
Another approach, which I often prefer, is to output shell commands via print, which I then, after close inspection, pipe into another shell.