in reply to Re: open with pipe
in thread open with pipe

Simple and innocent looking backticks become a nightmare once you write code for more than one system. Some problems, some general, some with your example:

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^3: open with pipe
by ikegami (Patriarch) on Aug 01, 2010 at 16:59 UTC

    You have to work with an unknown shell. Many shells behave according to POSIX, others don't. You don't know what shell you get. You can't write reliable code this way.

    It's sh except on Windows, and that's easy to check.

      Sure it's /bin/sh, but what shell do you get there?

      Most Linux systems have a symlink to /bin/bash there, most times giving you a bash v1, v2, v3 or v4. Newer Debian and Ubuntu versions have a symlink to /bin/dash, which is a Debian modified ash. Other Linux distributions symlink to the original ash or the busybox implementation of ash. Some BSDs have ash, others pdksh. Solaris has a bourne shell there. Some Mac OS X versions have a zsh there. Other operating systems have a korn shell as /bin/sh.

      See http://www.in-ulm.de/~mascheck/various/shells/ and the pages linked from there for the ugly details, especially http://www.in-ulm.de/~mascheck/bourne/ for the bourne shell variants, and http://www.in-ulm.de/~mascheck/various/ash/ for the ash family.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Wow! I suppose some of those shells are incompatible with the bourne shell?
Re^3: open with pipe
by dasgar (Priest) on Aug 01, 2010 at 13:50 UTC

    @afoken,

    Thanks for expanding the list of reasons why this is not the best idea of what to do for all cases. I knew there were probably more reasons than I could think of as to why this method could lead to trouble. As I mentioned, I do consider this a "quick and dirty" method because it can lead to lots of problems if you're not careful.

    So far, I've been too lazy to try to figure out how to properly use the IPC::Open3 with proper error trapping. I tried unsuccessfully a few times and then gave up and used back ticks instead. I've also been lucky enough so far to not have been bitten in the rear when I've used back ticks. :D

    Also, I could be wrong, but I believe that some of your cautionary warnings about OS specific commands and syntax would apply to any method in any programming language when trying to call system commands.

    And yes, I probably put up a poor example. Considering it was about 12:30 am in my time zone when I posted, I should have waited until I was thinking more straight.