Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 COMP111 Lecture 20 / Slide 2  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)

3 COMP111 Lecture 20 / Slide 3 Changing Directories (2)  Like other functions, parentheses are optional: $ cat cd1 #!/usr/local/bin/perl5 -w print "where to go? "; chomp($where = ); 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 $

4 COMP111 Lecture 20 / Slide 4 Globbing (1)  The shell takes a * and turns it into a list of all the filenames in the current directory.  [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.

5 COMP111 Lecture 20 / Slide 5 Globbing (2)  Just put the globbing pattern between angle brackets or use the glob function: @all = ; @b = ; @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).

6 COMP111 Lecture 20 / Slide 6 Globbing (3)  Example: $ ls addressletter1namessort $ cat glob1 #!/usr/local/bin/perl5 -w @files = ; foreach $file (@files){ print "$file\n"; } $ glob1 address letter1 $

7 COMP111 Lecture 20 / Slide 7 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 = ; foreach $file (@files){ print "$file "; } print "\n"; $ glob2 /etc/hostname.le0 /etc/hosts /etc/hosts.allow /etc/hosts.deny $

8 COMP111 Lecture 20 / Slide 8 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 = ; foreach $file (@files){ $file =~ s#.*/##;# delete to last slash print "$file "; } print "\n"; $ glob3 hostname.le0 hosts hosts.allow hosts.deny $

9 COMP111 Lecture 20 / Slide 9 Globbing (6)  Multiple patterns are permitted inside the glob: @bill_files = ;  The argument to glob is variable interpolated: $ cat glob4 #!/usr/local/bin/perl5 -w if(-d "/homes/horner/111"){ $where = "/homes/horner/111"; }else{ $where = "/homes/horner"; } @files = ; print "@files\n"; $ glob4 /homes/horner/111/letter1 /homes/horner/111/names

10 COMP111 Lecture 20 / Slide 10 Removing a File (1)  The Perl unlink function deletes a file, exactly like the UNIX rm command. unlink("bill");# bye bye bill  Example: $ ls letter1namesunlink1 $ cat unlink1 #!/usr/local/bin/perl5 -w print "what file to delete? "; chomp($what = ); unlink($what); $ unlink1 what file to delete? letter1 $ ls namesunlink1 $

11 COMP111 Lecture 20 / Slide 11 Removing a File (2)  unlink can take a list of names as well: unlink("bill", "gates"); unlink ;# delete all.cpp files  The return value on unlink is the number of files successfully deleted. foreach $file ( ){ if(unlink($file) == 0){ print "trouble deleting $file\n"; }

12 COMP111 Lecture 20 / Slide 12 Removing a File (3)  With no arguments to unlink, $_ is used as the default. foreach ( ){ if(unlink == 0){ print "trouble deleting $_\n"; }

13 COMP111 Lecture 20 / Slide 13 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"; }

14 COMP111 Lecture 20 / Slide 14 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");

15 COMP111 Lecture 20 / Slide 15  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

16 COMP111 Lecture 20 / Slide 16  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

17 COMP111 Lecture 20 / Slide 17  Here is the skeleton of a program for recursive directory processing: #!/usr/local/bin/perl5 -w sub dirtree { my @files = ; # local variable required print "Directory listing for: $_[0]\n"; foreach (@files){ print "$_\n"; dirtree($_) if (-d $_); # recursion } dirtree($ARGV[0]); Recursive Directory Processing


Download ppt "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."

Similar presentations


Ads by Google