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

What is considered best practice for specifying an executable in system() or exec()? My objective is write portable code that uses the user's executable search path while at the same time complex escaping of arguments to the executable. Regards Arthur
  • Comment on Best practices for executable name in system() or exec()

Replies are listed 'Best First'.
Re: Best practices for executable name in system() or exec()
by ikegami (Patriarch) on Feb 11, 2011 at 17:24 UTC

    If you just want to execute a program rather then a shell command, then you can use the multiple argument form of either, and you won't have to worry about escaping.

    system('cat foo bar'); # Cats 'foo' and 'bar' system('cat', 'foo bar'); # Cats 'foo bar' system('some command'); # Executes 'some' system({ 'some command' } 'some command'); # Executes 'some command'

    Some escapes aren't possible in Windows, so avoid " and control characters.

Re: Best practices for executable name in system() or exec()
by ww (Archbishop) on Feb 11, 2011 at 16:02 UTC
    Best practice?

    Depends on what you need to do.

    system ne exec

    So, we'll need a bit more information.

Re: Best practices for executable name in system() or exec()
by jethro (Monsignor) on Feb 11, 2011 at 17:16 UTC

    If your objective is to use the users search path, obviously you have to avoid giving absolute paths, instead you just have to give the name of the executable. Since you have only one choice in this regard, best practice == the one and only practice.