Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: backticks and system() (Updated)

by thanos1983 (Parson)
on Mar 31, 2019 at 11:04 UTC ( #1231919=note: print w/replies, xml ) Need Help??


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

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!

Replies are listed 'Best First'.
Re^4: backticks and system()
by haukex (Chancellor) on Mar 31, 2019 at 22:26 UTC
    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.

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2020-05-30 18:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?