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

Hello, Monks
I need a CGI programm which checks some condition, runs another program if the condition is true then redirects a user to a web page and exits. So I wrote this:
#!/usr/bin/perl -w if(condition){ exec '/usr/.../another.pl'; } print "Location: http://test/ \n\n";
It executes another.pl but the print statement is never reached as the 'exec' stops running the programm. So I changed the programm like this:
#!/usr/bin/perl -w print "Location: http://test/ \n\n"; if(condition){ exec '/usr/.../another.pl'; }
This one perfoms 'Location' but 'exec' is never reached and I don't understand why. The condition here is always true.

Could anyone explain me that and advise what I shall do?
Thank you.

Replies are listed 'Best First'.
Re: 'exec' & 'Location' problem
by Abigail-II (Bishop) on Sep 12, 2003 at 09:13 UTC
    It executes another.pl but the print statement is never reached as the 'exec' stops running the programm.

    Did you bother to lookup in the documentation what exec does? If you had, you would a) not be surprised by the behaviour, b) known what you should have done instead.

    This one perfoms 'Location' but 'exec' is never reached and I don't understand why. The condition here is always true.

    Then you must have found a bug in perl. Could you create a small, stand-alone, program that exhibits this bug?

    Abigail

Re: 'exec' & 'Location' problem
by Roger (Parson) on Sep 12, 2003 at 10:23 UTC
    exec will never exit, this is the expected behaviour. There are many ways to execute external programs in perl.

    Example 1:
    #!/usr/bin/perl -w if (condition) { system '/usr/.../another.pl'; } print "Location: http://test/ \n\n";
    Example 2:
    #!/usr/bin/perl -w if (condition) { `/usr/.../another.pl` # notice the use of the back quote. } print "Location: http://test/ \n\n";
    There are other methods too, like using Apache mod_perl's spawn_sub_process, etc.
      exec will never exit

      That's not quite true. The manual page says:

      exec PROGRAM LIST The "exec" function executes a system command and never returns-- use "system" instead of "exec" if you want it to return. It fails and returns false only if the command does not exist and it is exe- cuted directly instead of via your system's com- mand shell (see below).

      It can fail, but that's an exception.

      Abigail

        Thanks Abi for pointing out that exec will return false upon error. I assumed too much and overlooked the fact that the external program might not exist.
      Be sure to read the perldocs on the 'system' and backticks commands:
      perldoc -q backticks perldoc -q system perldoc -f system
      so that you're aware of all the implications - security and otherwise. If you use 'system', be aware that system does give back a return code, and, there is a 'more right' way to execute 'system' (and backticks) so that the shell is not involved.

      HTH.