How to make cd into a directory using perl?

I am trying to do the following. system "cd directoryfolder" but it fails, also I try to exit the terminal "exit", but it fails.

+4
source share
3 answers

code:

chdir('path/to/dir') or die "$!"; 

Perldoc:

  chdir EXPR chdir FILEHANDLE chdir DIRHANDLE chdir Changes the working directory to EXPR, if possible. If EXPR is omitted, changes to the directory specified by $ENV{HOME}, if set; if not, changes to the directory specified by $ENV{LOGDIR}. (Under VMS, the variable $ENV{SYS$LOGIN} is also checked, and used if it is set.) If neither is set, "chdir" does nothing. It returns true upon success, false otherwise. See the example under "die". On systems that support fchdir, you might pass a file handle or directory handle as argument. On systems that don't support fchdir, passing handles produces a fatal error at run time. 
+22
source

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 .

+14
source

I always like to mention File::chdir for cd -ing. It allows you to change the working directory, which is local to the closing block.

As Peder mentions, your script is basically all the system calls related to Perl. I present a more realistic Perl implementation.

 "wget download.com/download.zip"; system "unzip download.zip" chdir('download') or die "$!"; system "sh install.sh"; 

becomes:

 #!/usr/bin/env perl use strict; use warnings; use LWP::Simple; #provides getstore use File::chdir; #provides $CWD variable for manipulating working directory use Archive::Extract; #download my $rc = getstore('download.com/download.zip', 'download.zip'); die "Download error $rc" if ( is_error($rc) ); #create archive object and extract it my $archive = Archive::Extract->new( archive => 'download.zip' ); $archive->extract() or die "Cannot extract file"; { #chdir into download directory #this action is local to the block (ie {}) local $CWD = 'download'; system "sh install.sh"; die "Install error $!" if ($?); } #back to original working directory here 

In this case, two non-core modules are used (and Archive::Extract only has a kernel with Perl v5.9.5), so you may have to install them. To do this, use the cpan utility (or ppm on AS-Perl).

+3
source

All Articles