It still seems odd to me that the code called by exec() runs basically instantly while using backticks it literally takes several minutes to complete the task and print the output. There must be something I'm overlooking.
From what you said, it sounds like backticks actually use the exec() function -- unless you meant something else. Is that the case?
"the two programs aren't at all equivalent, since a successful call to exec() never returns. (You probably know that, but still)."
Yeah, I was aware of that, and that's one of the reasons I tried the backticks first (in case I ended up doing more after running the shell command). It looks like I'll just have to make sure running that command is the absolute last thing the script does, unless I want to either write a lot more code to get the same result as intended with backticks (but faster) or suffer some really unacceptable performance lags on a simple admin script.
| print substr("Just another Perl hacker", 0, -2); |
|
- apotheon
CopyWrite Chad Perrin |
| [reply] |
How long does it take to run the command manually? It might just take very long to finish.
backticks will wait for the program to end before returning the output, so if the program just takes a long time to stop, you won't see any output since you're capturing it with the backticks. exec will just replace the current program with the new one, so any output of the command will be printed immediately, even if the program is still running.
by the way, if you're not doing anything with the output except printing you could also use system(), which doesn't capture the program's output, so any output will also be printed immediately (while the program is running, before system() returns).
About backticks using exec() - exec() is the only way to start a new program in unix. system(), backtics, piped open etc all use exec() behind the scenes. Calls that execute a command and return do a fork first, then call exec() in the child process. That's why exec() should always be the fastest. Not that you'd normally notice the speed difference - fork() is pretty fast (it has to be - it's the only way to start a new process).
| [reply] |
Running the program manually takes effectively zero seconds, as indicated by my mentions of the fact that running it with exec() is essentially instantaneous.
Your reference to exec() being the "only way to start a new program" makes me think you're talking about the system call exec(), as opposed to the Perl function (which calls the unix system call). Is that what was meant in the previous post, then? I thought it was referring to the Perl function.
| print substr("Just another Perl hacker", 0, -2); |
|
- apotheon
CopyWrite Chad Perrin |
| [reply] |