Originally Posted by FishMonger
You haven't clearly stated it, but it appears that both of your questions involve attempting to alter the environment of the parent process, which as I mentioned in your other question, can't be done in the Perl script.
Yes, Fishmonger, I also strongly suspected that this was the aim, but I just wanted to point out that the starting point of the OP's description of the issue was incorrect: it is perfectly possible call shell internal commands (pwd, kill, echo, cd, ls, etc.). But the result is not always what is expected (as can be seen below).
To the OP: why is it then impossible to change environment variables or modify the environment with a ulimit command? The main reason is that when you try to "shell out" to do that with a system or a `...` command, Perl is is forking, i.e. is creating a new sub-process, and any environment change that you do in that sub-process might be seen only within that sub-process, but not in the parent process once the sup-process has died.
Just another example under the debugger of the nature of the problem:
- Perl Code
DB<2> `cd ..`
The program apparently did not move up one directory. In fact, it did. But once the thread died, we were back in the original execution Perl environment.
Just a last example, in fact almost the same with a slight variation:
- Perl Code
DB<4> $c = `pwd`
DB<6> `cd ..; ls; pwd`
DB<7> @d = `cd ..; ls`
DB<8> x @d
Instructions 4 and 5 are the same we have already seen.
Instruction 6 does something more complex: move up one directory and list the content and print the current directory. THIS WORKS: the @d variable shows that ls command actually does list the content of the directory above (in this case, there is only on entry in the /home) and then displays the current directory.
But once this is finishes, we are back where we were.
Similarly, you might launch a complete shell script from a system or `...` (or an executable program, or whatever), but changes to the execution environment will be seen only within that sub-process. But some changes made by that script will be permanent: say, if you delete a file in this script, clearly, the file will be no longer there.
Well, it is slightly more complicated in reality and there may be in some cases ways of getting around that (this is rarely very pretty programming, but sometimes you have to live with that), but this is really getting into shell programming rather than Perl programming, which is not the topic of this forum, and we don't know really what you want to do.
As an example, you could run from Perl a shell script that changes the ulimit thing and happily runs with that changed environment. Sometimes, it is all what you need. But, by now, I hope that you understand that these changes will generally not apply to the parent Perl process.