in reply to Running Entire Bash Script Inside Perl

It might not be directly relevant to your question (I'm not sure), but check out this old node of mine: shloop -- execute shell command on a list. If nothing else, it might give you some ideas about how to do whatever it is you actually want to do.

As for what you've posted, if you put the content of your __DATA__ block into an disk file, e.g. call it "foo.sh", then do system( "foo.sh" ) or maybe system( "/bin/sh", "foo.sh" ) then the subshell run by the system call would read the shell script from that file, and run it.

If the idea is to use perl to change some string in the shell script and then run it, then the trick I used in shloop might help you: open up a pipeline file handle that runs a shell, and then print shell commands to it:

open( SH, "|-", "/bin/sh" ) or die "can't launch a shell: $!\n"; my $param = "whatever"; print SH "echo hello\n"; print SH "echo $param\n"; print SH "echo hi\n"; close SH;

Replies are listed 'Best First'.
Re^2: Running Entire Bash Script Inside Perl
by ikegami (Patriarch) on Jun 19, 2009 at 23:33 UTC

    That design requires advance knowledge of the script (to change $1 to the parameter). I don't know if that's a problem.

    The implementation fails if $param contains two spaces in a row, a quote, etc due to improper encoding of strings. Fix:

    sub to_sh_lit { my ($s) = @_; die if !utf8::downgrade($s, 1); # Expecting bytes. die if $s =~ /\x00/; # NUL can't be passed. $s =~ s/('+)/'"$1"'/g; return "'$s'"; } my $lit_param = to_sh_lit($param); print SH "echo $lit_param\n";

    Update: Added code.

      That design requires advance knowledge of the script (to change $1 to the parameter).

      Well, there's no telling from the OP (as originally seen) how much is known in advance. Whatever the requirements are for the task, this approach could be adapted to use the available knowledge as needed, and work as well as other approaches.

      The implementation fails if $param contains two spaces in a row, a quote, etc.

      Sure. When printing commands to a shell process, there are lots of ways to go wrong and fail (and/or cause all sorts of mayhem and damage, depending on what sorts of mistakes are made and how various permissions play out when the perl script executes). This approach demands respect and caution; anyone who isn't sure whether they might get it wrong probably shouldn't use it.