COPSoftwareToolsPerlFile andDirectoryAccess
Perl File and Directory Access Software Tools
mPSlide2Changing DirectoriesYou can change the current working directorywithinPerl,just like thecdshellcommandInPerl,thechdirfunction,takesa singleargument -- the directory name to change tochdir returns true ifyou've successfullychanged tothe requested directory,and false ifyou could not.chdir("/etc")ll die "cannot cd to /etc
Slide 2 Changing Directories You can change the current working directory within Perl, just like the cd shell command. In Perl, the chdir function, takes a single argument - the directory name to change to. chdir returns true if you’ve successfully changed to the requested directory, and false if you could not. chdir("/etc") || die "cannot cd to /etc";
nPSlide3Changing DirectoriesLikeotherfunctions,parenthesesareoptional:$catcdl#!/usr/local/bin/perl5-wprint"where to go? ";chomp(Swhere=<STDIN>);if(chdir swhere)(print "chdir succeeded, now in swhereln";lelse(print "chdir did not succeededln";$cdlwhere to go? /binchdir succeeded,now in /binS
Slide 3 Changing Directories Like other functions, parentheses are optional: $ cat cd1 #!/usr/local/bin/perl5 -w print "where to go? "; chomp($where = <STDIN>); if(chdir $where){ print "chdir succeeded, now in $where\n"; }else{ print "chdir did not succeeded\n"; } $ cd1 where to go? /bin chdir succeeded, now in /bin $
mPSlide4Changing DirectoriesWhenyour Perl program starts,the currentworking directory will be inherited from the shellthat invoked it.However, using chdir in Perl will not change theworking directory of the shell whenyour programends.The chdir function without a parameter defaultsto taking you to your home directory
Slide 4 Changing Directories When your Perl program starts, the current working directory will be inherited from the shell that invoked it. However, using chdir in Perl will not change the working directory of the shell when your program ends. The chdir function without a parameter defaults to taking you to your home directory
mPSlide5GlobbingThe shell takes a * and turns it into a list of all thefilenames in the current directory.Similarly, [a-m] * . cpp turns into a list of allfilenames inthe current directory that begin witha letter in the first half of the alphabet, and end in.cpp.Theexpansionof * or/etc/host* into the listof matching filenames is called globbing
Slide 5 Globbing The shell takes a * and turns it into a list of all the filenames in the current directory. Similarly, [a-m]*.cpp turns into a list of all filenames in the current directory that begin with a letter in the first half of the alphabet, and end in .cpp. The expansion of * or /etc/host* into the list of matching filenames is called globbing