Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: Poll: Is your $^X an absolute path? (system @list)

by tye (Sage)
on Aug 18, 2006 at 12:19 UTC ( #568118=note: print w/replies, xml ) Need Help??


in reply to Re^4: Poll: Is your $^X an absolute path? (argv[0])
in thread Poll: Is your $^X an absolute path?

It'd be nice if y'all fixed system(@list) to work reasonably on Win32. See Re: The Evil Embedded Space (system(@list))

- tye        

  • Comment on Re^5: Poll: Is your $^X an absolute path? (system @list)

Replies are listed 'Best First'.
Re^6: Poll: Is your $^X an absolute path? (system @list)
by xdg (Monsignor) on Aug 18, 2006 at 13:55 UTC

    I think the safe thing is to always surround the first argument with double-quotes if more than one argument is provided and it doesn't already seem to have double quotes. (If only one argument is provided, we have to assume the user provided a valid command line on the current platform.) So, ignoring error reporting, something like this:

    sub system { $_[0] = qq{"$_[0]"} if @_ > 1 && substr($_[0],0,1) ne q{"}; CORE::system( @_ ); }

    On reflection, that seems simple enough that maybe we can get some p5p person to patch it.

    Some references about how spaces are handled in program names:

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      assume the user provided a valid command line on the current platform

      Which translates to "avoid portability". I strongly disagree.

      Look at existing attempts to provide portability with command lines in Perl. Several Perl subsystems compose lists of which command to use to do which task and then expect system($command{copy},$from,$to) to work. That is quite reasonable. Too bad it doesn't work very well on Win32. But that is because system(@list) has always been broke on Win32, depriving an important tool for dealing with command lines portably in Perl.

      Unfortunately, we can't make system(@list) work portably for all Win32 commands but we can make it work portable for most commands, and we should do that.

      But you do remind me of a point that I had previous included in my proposal but that I didn't include in the one I linked to. We should probably not add quotes if there are already quotes around the argument. I can see advantages to either way so perhaps it should be an option, but the default should be to avoid adding quotes if they are already there.

      - tye        

        I go back to perlport:

        Interprocess Communication (IPC) In general, don't directly access the system in code meant to be portable. That means, no "system", "exec", "fork", "pipe", ``, "qx/ +/", "open" with a "|", nor any of the other things that makes being a p +erl hacker worth being.

        Relying on external programs is fundamentally non-portable, even if it works much of the time. Vanilla Perl has made me very aware of just how fragile lots of the assumptions about "make", "nmake" and "dmake" are.

        I agree totally that we should try to be helpful in the case of system(@list) and ensure the first argument is quoted if its not. But the semantics for system($line) are messy.

        Should we do the same workaround as CreateProcess and walk the command line, joining up spaces into the first argument until we find something that can execute and then wrap that in quotes?

        sub auto_quote_system { my $line = shift; my @parts = split " ", $line; my $cmd = shift @parts; while ( ! -x $cmd ) { # does -x works for file associations? $cmd .= " " . shift @parts; } return qq{"$cmd" @parts}; }

        Even that's not complete. It doesn't deal well with multiple spaces in an executable path nor with commands that can't be found. And note the unexpected result or trojan potential of a program called C:\program.exe. I think that could quickly wind up with a very convoluted kludge.

        I'm more comfortable saying that if you call system($line) then it's up to you to make sure that the line is valid for your OS, particularly if we can patch system and perlport docs to advise that wrapping the first argument in a quotes is a good idea.

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://568118]
help
Chatterbox?
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2018-07-22 14:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (454 votes). Check out past polls.

    Notices?