The reason you cannot do this by calling system is because system will start a new process, execute your command, and return the exit status. So when you call system "cd foo" , you start the shell process, which switches to the "foo" directory and then exits. Nothing will happen in your perl script. Similarly, system "exit" will start a new process and exit immediately.
What you want for the cd case is - as bobah points out - the chdir function. There is an exit function to exit your program.
However, none of them will affect the state of the terminal session in which you are located. After completing your perl script, the working directory of your terminal will be the same as before you started it, and you cannot exit the terminal session by calling exit in your perl script.
This is because your perl script is again a separate process from your terminal shell, and everything that happens in separate processes usually does not interfere with each other. This is a function, not an error.
If you want changes to occur in your shell environment, you must issue instructions that are understood and interpreted by your shell. cd is a built-in command in your shell like exit .
source share