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

Hi,

I have this C++ program which reads in this large binary file, does some cpu and memory intensive calculations, and then prints two numbers to the STDOUT on one line. Now when I run this C++ program using a system call everything runs fine and I get the 2 numbers on the STDOUT that I am looking for. With the backtick operator however I get a memory segmentation violation. What is different in the way perl runs system and backtick ? I thought both of them do a exec, fork, and wait. I am not sure if the memory leak is in my program now or the perl backtick operator. I can repeat this segmentation violation only with the backtick operator in a perl script. The C++ program runs fine off the command line, and the perl system call. I am throughly confused. Any help from you monks would be greatly appreciated.

I am running perl version 5.005_02 built for sun4-solaris (activestate built).

Thanks,
Waris

Replies are listed 'Best First'.
Re: backtick question
by broquaint (Abbot) on Jan 22, 2003 at 20:57 UTC
    I believe this is and old bug and has been fixed in later versions of perl. Checkout the p5p archive for info on your particular problem (perhaps this and this might be of some enlightenment). My advice would be to upgrade your version of perl.
    HTH

    _________
    broquaint

Re: backtick question
by graff (Chancellor) on Jan 22, 2003 at 20:39 UTC
    Depending on how you do the system call and the backtick call, there may be subtle differences in how perl and/or the sub-shell are interpolating things that look like variables (or are not interpolating things that you believe should be treated as variables). It would be helpful if you could post the actual system and backtick lines from your code, along with any relevant snippets that assign values to any variables involved.

      Specifically, when you use backticks or call system() with a scalar argument, the argument is passed to the system shell (typically /bin/sh). If you call system() with an array argument, the args are executed directly.

      This has important security implications because the system shell will interpolate metacharacters. See 37385 for a more detailed explaination.

      Try this and see if it works:

      if (open(PROGRAM, "-|")) { # Parent process. Read output from child. my @output = <PROGRAM>; } else { # Child process. exec() program exec("/path/to/program", $arg1, $arg2, $etc); }

      -Matt

Re: backtick question
by OM_Zen (Scribe) on Jan 22, 2003 at 20:52 UTC
    Hi

    The

    system ("cat");


    command actually creates a child process (a shell) and then calls the exec function , while

    exec "cat";


    by itself executes the string in the current shell , I think the backtick has the same difference with system