perl file and directory access learning objectives: 1. to learn how to change directories being...
Post on 19-Dec-2015
217 views
TRANSCRIPT
Perl File and Directory Access
Learning Objectives:1. To learn how to change directories being
accessed in a Perl program
2. To learn the Perl’s command involving file system management / accessing
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 22
Perl File and Directory AccessTable of Content Changing Directories Globbing Removing a File Renaming a File Hard Links Soft Link Making and Removing Directories Permissions Modifying Permissions Recursive Directory Processing
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 33
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";
Changing Directories (1)
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 44
Changing Directories (2) Like other functions, parentheses are optional:
$ cat cd1#!/usr/local/bin/perl5 -wprint "where to go? ";chomp($where = <STDIN>);if(chdir $where){
print "chdir succeeded, now in $where\n";}else{
print "chdir did not succeeded\n";}$ cd1where to go? /binchdir succeeded, now in /bin$
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 55
Changing Directories (3)
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.
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 66
Globbing (1)
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.
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 77
Globbing (2)
Perl supports globbing. Just put the globbing pattern between angle brackets or
use the glob function:
@all = <*>;@b = <[a-m]*.cpp>;@c = glob("[a-m]*.cpp"); # same as @b
In a list context, the glob returns a list of all names that match the pattern (or an empty list if none match).
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 88
Globbing (3)
Example:$ lsaddress letter1 names sort$ cat glob1#!/usr/local/bin/perl5 -w@files = <[a-m]*>;foreach $file (@files){
print "$file\n";}$ glob1addressletter1$
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 99
Globbing (4)
If you use a full pathname in your glob, you will get full pathnames as a result:
$ cat glob2#!/usr/local/bin/perl5 -w@files = </etc/host*>;foreach $file (@files){
print "$file ";}print "\n";$ glob2/etc/hostname.le0 /etc/hosts /etc/hosts.allow
/etc/hosts.deny$
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1010
Globbing (5)
If you want just the simple filename, you can use substitute to chop off the directory part of the string:
$ cat glob3#!/usr/local/bin/perl5 -w@files = </etc/host*>;foreach $file (@files){$file =~ s#.*/##; # delete to last slashprint "$file ";}print "\n";$ glob3hostname.le0 hosts hosts.allow hosts.deny$
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1111
Globbing (6)
Multiple patterns are permitted inside the glob:@bill_files = <gates* clinton*>;
The argument to glob is variable interpolated:$ cat glob4#!/usr/local/bin/perl5 -wif(-d "/homes/horner/111"){
$where = "/homes/horner/111";}else{
$where = "/homes/horner";}@files = <$where/*>;print "@files\n";$ glob4/homes/horner/111/letter1 /homes/horner/111/names
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1212
Removing a File (1)
The Perl unlink function deletes a file, exactly like the UNIX rm command.
unlink("bill"); # bye bye bill
Example:$ lsletter1 names unlink1$ cat unlink1#!/usr/local/bin/perl5 -wprint "what file to delete? ";chomp($what = <STDIN>);unlink($what);$ unlink1what file to delete? letter1$ lsnames unlink1$
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1313
Removing a File (2)
unlink can take a list of names as well:
unlink("bill", "gates");
unlink <*.cpp>; # delete all .cpp files
The return value on unlink is the number of files successfully deleted.
foreach $file (<*.cpp>){
if(unlink($file) == 0){
print "trouble deleting $file\n";
}
}
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1414
Removing a File (3)
With no arguments to unlink, $_ is used as the default.
foreach (<*.cpp>){
if(unlink == 0){
print "trouble deleting $_\n";
}
}
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1515
Renaming a File
The Perl function rename allows you to rename files.
Here is how to rename the file gates into cheap:rename("gates", "cheap");
rename returns a true value if successful. if(rename("gates", "cheap")){
print "Bill is now cheap\n";
}else{
print "Bill is still gates\n";
}
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1616
Hard Links
The Perl function link allows you to create a hard link.
Here is how to link from the file gates to cheap:link("gates", "cheap");
link returns true if successful. if(link("gates", "cheap")){
print "Bill is now also cheap\n";
}else{
print "Bill is still only gates\n";
}
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1717
Soft Links
The Perl function symlink allows you to create a soft (symbolic)
link.
Here is how to symbolic link from gates to cheap:symlink("gates", "cheap");
readlink returns the name pointed at by the specified symbolic
link:symlink("gates", "cheap");
$x = readlink("cheap");
print "cheap points at $x\n"; #cheap points at gates
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1818
Making and Removing Directories
The Perl functions mkdir and rmdir allow you to make and
remove directories.
mkdir takes the name of the new directory and a mode that
determines the permissions
Use 0755 for the mode to allow user full (rwx) permission, and
no write permission for group and other (rx only). mkdir("gatesdir", 0755);
rmdir("gatesdir");
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 1919
The leading mode number is always 0, and the other 3 numbers
are permissions for user, group, and other respectively.
The mode values are octal, and have the following meanings: 0 ---
1 --x
2 -w-
3 -wx
4 r--
5 r-x
6 rw-
7 rwx
Permissions
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 2020
The Perl function chmod allows you to change file and
directory permissions:
mkdir("gatesdir", 0755); #u:rwx g:rx o:rx
chmod(0750, "gatesdir"); #u:rwx g:rx o:
chmod(0531, "gates"); #u:rx g:wx o:x
chmod(0642 , "gates", "cheap"); #u:rw g:r o:w
Modifying Permissions
COMP111COMP111Lecture 20 / Slide Lecture 20 / Slide 2121
Here is the skeleton of a program for recursive directory
processing:
#!/usr/local/bin/perl5 -wsub dirtree { my @files = <$_[0]/*>; # local variable required print "Directory listing for: $_[0]\n"; foreach (@files){ print "$_\n"; dirtree($_) if (-d $_); # recursion }}dirtree($ARGV[0]);
Recursive Directory Processing