in reply to Heredoc with system call
$their_stdout = <<`END_CMDS`; die "oops: wstat $?, errno $!" if $?; ( echo set password 'oracle'; echo set current_listener 'LISTENER'; echo status; ) | lsnrctl END_CMDS
Well, ok: just because you can backtick your heredoc, doesn't automatically mean you (always?) should. It does seem a wee bit convoluted over using `shell commands` or qx{shell commands} directly. Remember you don't want system(shell commands) if you must inspect the command's (or commands') output. The system function is reserved for those (perhaps fewer) scenarios when you care only for the process's (or subshell's) exit status, disregarding whatever that command may splutter on its STDOUT and STDERR, leaking out to the user.
Like any normal situation in Perl, the default behaviour is to replace (certain) variables with their values -- unless and until you ask this not to happen. The pseudo-literal `shell commands` always interpolates, whether in normal use or applied to the <<`END_TOKEN` string.
Maybe, probably, perhaps that's ok. Usually.
But with backticks' alias to `shell commands` via the nifty pick-your-own-delimiters qx{shell commands} notation, now you decide where, when, and whether entire strings or individual variables expand.
Much more flexible this way: if you need some things to interpolate but others not to, choose interpolative syntax and "escape" those you don't want to expand by \backslashing them.
# variables normally expand $bunches = "\t@these + @those\n"; # but not if escaped; the hats are literals now $bunches = "\t\@these + \@those\n"; # or suppress them all in one swell foop $cost = '39 widgets @$1.59 apiece'; # selectively suppressed variable expansion system qq{Mail -s "gotta problem" helpdesk\@support <$complaints}; # variables normally expand @many = ( @these, @those); # suppress variable expansion @rpair = (\@these, \@those); # *distributively* suppress variable expansion @rpair = \(@these, @those);
You're free to delimit the commands with suppressive single-quotes via the qx'shell commands' form, just as with all these and more:
Contrary to popular misunderstanding, even hash subscripts or the RHS of the double-barrelled comma may sometimes need quoting. These are different:
$place{ 040 } = 1; $place{'040'} = 2; # similarly... %place = ( 040 => 1, '040' => 2, );
This all assumes you want the main program to pause until the subshell exits. Asynchronous executions through variations of perlian cognates to popen and kin are also possible, but don't seem especially applicable to what you appear intent on doing.
HTH && HAND || SAD;
|
---|