I am trying the following.. system \"cd directoryfolder\" but it fails, also I try system \"exit\" to leave the terminal but it fails.
The reason you can't do those things by calling system
is that system
will start a new process, execute your command, and return the exit status. So when you call system "cd foo"
you will start a shell process, which will switch to the "foo" directory and then exit. Nothing of any consequence will happen in your perl script. Likewise, system "exit"
will start a new process and immediately exit it again.
What you want for the cd case, is - as bobah points out - the function chdir
. For exiting your program, there is a function exit
.
However - neither of those will affect the state of the terminal session you are in. After your perl script finishes, the working directory of your terminal will be the same as before you started, and you will not be able to 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 things that happen in separate processes generally do not interfere with each other. This is a feature, not a bug.
If you want things to change in your shell environment, you must issue instructions that are understood and interpreted by your shell. cd
is such a builtin command in your shell, as is exit
.