Score:-1

in perl, the command "system", using indirect object, redirection does not work, need help

ws flag

I am running Kubuntu 20.04. The perl version is shown by perl -v to be

perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux-gnu-thread-multi (with 50 registered patches, see perl -V

I have just made apt-get update and with Muon package manager I have all installed perl packages upgraded.

But after that, perl is 5 as before, I have not got perl 6.

My problem is not when I use the simple system command like

perl -we 'system("echo \"aaa\" > /tmp/WEx_args_file"); '

In this case the file is written.

But when using the indirect object way, like

perl -we ' @WE_args = ( "echo", " \"qqq\" > /tmp/WEx_args_file" ); $WE_retcod_system = system { $WE_args[0] } @WE_args; print "WE_retcod_system: ~", $WE_retcod_system, "~\n"; if($WE_retcod_system != 0) { die "\nsystem >>>YYY>>>@WE_args<<<YYY<<< failed: $?"; }; '

in this case the file is not written.

The displayed output is on screen:

"qqq" > /tmp/WEx_args_file

then follows

WE_retcode_system: ~0~

I have tried the indirect object way in many variations a whole day. I have not found a code the tmp file to be written.

Please, do anybody know a solution?

=====

In trying to proceed, I have found the very good

IPC::System::Simple

By means of this, my intention is to replace my backtick code

echo -n "qqq" | xxd -g1

with the capturex function, like

perl -we ' use IPC::System::Simple qw/capturex/; open(FHo, ">/tmp/Wex_args_file") or die; $WE_got_from_cpx = capturex("echo", "-n", ""qqq"", "|", "xxd -g1" ); print FHo "WE_got_from_cpx: ~", $WE_got_from_cpx, "~\n"; '

As far as I understood, this is the correct usage of capturex. But new problem, it does not work.

The command on the RHS of the pipe, i.e.

xxd -g1

is not executed but only copied. The result is

WE_got_from_cpx: ~"qqq" | xxd -g1~

I'm afraid that these modules in IPC::System::Simple are based on the leading point like the "indirect object" way that only the first argument is the executed command.

How can I use IPC::System::Simple to make the whole backtick function go?

Regards anton

muru avatar
us flag
Why did you pass `echo` as an argument by itself but combined `"qqq" > /tmp/WEx_args_file` into one argument?
Anton Wessel avatar
ws flag
The recommended, so called "indirect object" way implies that the command to be executed is written in such a format: must b
Anton Wessel avatar
ws flag
The recommended, so called "indirect object" way implies that the command to be executed is written in such a format: must be as predecessor before of the list follows, the list of command "system list". All intermediate writings do not work, except when reduced to my first case of simply sytem()..
raj avatar
cn flag
raj
Using indirect object syntax, you are passing `"qqq" > /tmp/WEx_args_file` as an argument string to the `echo` command, so no wonder that the command prints exactly that. In the simple version, the argument is only `\"aaa\"`. Redirection (`> /tmp/WEx_args_file`) is **not** part of the argument string; it is handled by the shell before arguments are passed to the command. You need to find a way to handle redirection using indirect object syntax; this may be tricky, if possible at all, will probably involve some operations on file descriptors. I suggest asking on StackOverflow for that.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.