Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: backticks and system() (Updated)

by thanos1983 (Parson)
on Mar 28, 2019 at 22:52 UTC ( [id://1231839]=note: print w/replies, xml ) Need Help??


in reply to backticks and system()

Hello ytjPerl,

From the documentation system:

my @args = ("command", "arg1", "arg2"); system(@args) == 0 or die "system @args failed: $?";

Have you tried something similar? (Not Recommended)

my @args = qw(Runbatch.exe -j dpsgl -r dpstst); system(@args) == 0 or die "system @args failed: $?";

Have you tried something similar? (Recommended)

my @args = ('Runbatch.exe', '-j', 'dpsgl', '-r', 'dpstst'); system(@args) == 0 or die "system @args failed: $?";

If there is an error on the command it will be printed through the die command.

Also for general information fellow Monk haukex has written a very interesting tutorial Re: curl without backticks and system() (updated x2).

Update: I forgot to add the reason that why my proposed solution I believe it will work for your case. Sample below:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @args = ('Runbatch.exe -j dpsgl -r dpstst'); my @argsUpdated = qw(Runbatch.exe -j dpsgl -r dpstst); print Dumper \@args, \@argsUpdated; __END__ $ perl test.pl $VAR1 = [ 'Runbatch.exe -j dpsgl -r dpstst' ]; $VAR2 = [ 'Runbatch.exe', '-j', 'dpsgl', '-r', 'dpstst' ];

Update 2: Based on useful links that haukex and afoken provided the system command that I presented above it looks to have the same affect as a single quote (string). So proposed solution if you wish to use system would be like this (see below):

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @args = ('Runbatch.exe -j dpsgl -r dpstst'); my @argsUpdated = ('Runbatch.exe', '-j', 'dpsgl', '-r', 'dpstst'); print Dumper \@args, \@argsUpdated; __END__ $ perl test.pl $VAR1 = [ 'Runbatch.exe -j dpsgl -r dpstst' ]; $VAR2 = [ 'Runbatch.exe', '-j', 'dpsgl', '-r', 'dpstst' ];

Where you explicitly define the list of commands.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: backticks and system() (Updated)
by haukex (Archbishop) on Mar 29, 2019 at 12:58 UTC

    Actually, system with a single string argument comes closer to qx// than the system LIST form (with LIST having more than one element), qx// has no built-in "list" form which avoids the shell like system LIST does. That's why I think the two commands as shown in the question should be pretty much equivalent; there's probably some information missing from the question, like maybe differing PATH settings, or maybe the command strings aren't exactly as shown, or something like that.

        Hello afoken,

        Thank you for the links they were very helpful to understand what was the affect of qx and system due to different behaviour of shells.

        BR / Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!

      Hello haukex,

      Thank you for the explicit analysis on this case I had no idea.

      One last question though. How do you know about all these rules and problems that they come up on all these commands? I mean by seeing the code or knowing by experience? I have read all the links that afoken provided and I was able to understand specifically what you mean but I am wondering how some of you know so deep regarding the commands. If someone would like to known more how he/she can learn that much?

      Correct me if I am wrong, but qw explicitly converts the strings inside the parenthesis to a list of strings right? I am confused about this part. If I come across a similar case I should explicitly define the list of commands or I can use qw?

      Thanks in advance for your time and effort.

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!
        One last question though. How do you know about all these rules and problems that they come up on all these commands?

        Mostly it was a detailed reading of system, exec, open, qx// in perlop, the relevant parts of perlipc, execvp(3), a few bits from perlport, as well as posts such as afoken's The problem of "the" default shell and e.g. this one by salva, plus some additional information that was linked to from those docs. I also recall testing things out with e.g. strace and looking at the various modules' code while writing this node.

        Correct me if I am wrong, but qw explicitly converts the strings inside the parenthesis to a list of strings right? I am confused about this part. If I come across a similar case I should explicitly define the list of commands or I can use qw?

        Yes, basically qw// splits on whitespace, so the two forms you showed in your node "my @args = qw(Runbatch.exe -j dpsgl -r dpstst);" and "my @args = ('Runbatch.exe', '-j', 'dpsgl', '-r', 'dpstst');" are identical and the "Not Recommended" / "Recommended" doesn't really apply.

        Note that qw// has nothing to do with qx//, they are two completely different operators that just happen to have similar names. qx// treats its argument as a single string, and you get the whitespace handling from the shell, so the same thing that happens at the command line when you type a command - this also means that whitespace can be quoted*, e.g. qx/echo "foo bar"/ is exactly like typing echo "foo bar" at the command prompt and would pass a single string "foo bar" to the command echo (assuming a "normal" shell). qw// just splits on whitespace, regardless of any quoting - qw/echo "foo bar"/ returns a list of three elements, ('echo', '"foo', 'bar"'). See also Quote Like Operators. Therefore, in e.g. system(qw/echo "foo bar"/), the shell would be avoided because system was passed more than one argument, and the echo command would receive two arguments instead of one.

        * However, getting that quoting right can get very complicated if there are special characters in the strings, which is why modules like ShellQuote::Any are a better approach, or, IMHO even better, just avoid the shell in the first place.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1231839]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found