Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: backticks and system()

by haukex (Archbishop)
on Mar 31, 2019 at 22:26 UTC ( [id://1231932]=note: print w/replies, xml ) Need Help??


in reply to Re^3: backticks and system() (Updated)
in thread backticks and system()

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.

Replies are listed 'Best First'.
Re^5: backticks and system()
by thanos1983 (Parson) on Apr 01, 2019 at 08:01 UTC

    Hello haukex,

    Thank your for your time and all the links that you provided me. Yes I also notice the difference between qx and qw. I also was reading this system() and qx comparison, which someone might also find it useful.

    Never the less thank you again and hopefully I will continue experimenting and I will be able to see the differences in shell. I need to test the commands on both OS to see what is happening.

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1231932]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found