##
open my $shell, "|/bin/sh" || die "$!\n";
print $shell "export var=value\n";
...
print $shell "some complicated shell command # ;(){:|:&};:\n";
print $shell "/run/the/program --with-args\n";
####
system <<"END";
export var=value
...
some() {shell commands}
/run/some/program
END