Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

backticks and system()

by ytjPerl (Scribe)
on Mar 28, 2019 at 18:11 UTC ( #1231823=perlquestion: print w/replies, xml ) Need Help??

ytjPerl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a window command "Runbatch.exe dpsgl -j dpsgl -r dpstst". directory is set. when I put this command in confi.txt and in perl, I use backticks to execute it, it works. but when I use system(), system('Runbatch.exe dpsgl -j dpsgl -r dpstst'), it does not work. it throws out error indicating this runbatch.exe not found. Can you advice why? I suppose system() and backticks function the same. Thanks.

Replies are listed 'Best First'.
Re: backticks and system()
by haukex (Bishop) on Mar 28, 2019 at 19:41 UTC

    Could you show a Short, Self-Contained, Correct Example script that reproduces the issue on your end, showing the command that works with backticks first, and then the system that isn't working? Otherwise we'd just have to guess what might be going wrong...

Re: backticks and system() (Updated)
by thanos1983 (Parson) on Mar 28, 2019 at 22:52 UTC

    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!

      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 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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231823]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2020-07-03 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?