Chapter 10 More Control Structures
Loop Controls
The redo Operator
# Typing test my @words = qw{ fred barney pebbles dino wilma betty }; my $errors = 0; foreach (@words) { ## redo comes here ## print "Type the word '$_': "; chomp(my $try = <stdin>); if ($try ne $_) { print "Sorry - That's not right.\n\n"; $errors++; redo; # jump back up to the top of the loop } } print "You've completed the test, with $errors errors.\n";
Labeled Blocks
LINE: while (<>) { foreach (split) { last LINE if /__END__/; # bail out of the LINE loop ... } }
The defined-or Operator
short-circuits when it finds a defined value, no matter if that value on the lefthand side is true or false. Even if someone’s last name is 0.
my $last_name = $last_name{$someone} // '(No last name)';
Chapter 11 Perl Modules
Using Simple Modules
Using Only Some Functions from a Module
use File::Basename qw/ basename /; use File::Basename ();
Chapter 12 File Tests
File Test Operators
File Test | Meaning |
-r | File or directory is readable by this (effective) user or group |
-w | File or directory is writable by this (effective) user or group |
-x | File or directory is executable by this (effective) user or group |
-o | File or directory is owned by this (effective) user |
-R | File or directory is readable by this real user or group |
-W | File or directory is writable by this real user or group |
-X | File or directory is executable by this real user or group |
-O | File or directory is owned by this real user |
-e | File or directory name exists |
-z | File exists and has zero size (always false for directories) |
-s | File or directory exists and has nonzero size (the value is the size in bytes) |
-f | Entry is a plain file |
-d | Entry is a directory |
-l | Entry is a symbolic link |
-S | Entry is a socket |
-p | Entry is a named pipe (a “fifo”) |
-b | Entry is a block-special file (like a mountable disk) |
-c | Entry is a character-special file (like an I/O device) |
-u | File or directory is setuid |
-g | File or directory is setgid |
-k | File or directory has the sticky bit set |
-t | The filehandle is a TTY (as reported by the isatty() system function; filenames can’t be tested by this test) |
-T | File looks like a “text” file |
-B | File looks like a “binary” file |
-M | Modification age (measured in days) |
-A | Access age (measured in days) |
-C | Inode-modification age (measured in days) |
Perl has a special shortcut to help you not do so much work. The virtual filehandle _ (just the underscore) uses the information from the last file lookup that a file test operator performed. Perl only has to look up the file information once now.
if (-r $file and -w _) { ... } if (-r $file) { print "The file is readable!\n"; } if (-w _) { print "The file is writable!\n"; } # stack the file test use 5.010; if (-w -r $file) { print "The file is both readable and writable!\n"; }
The Stat and lstat Functions
If you need the (mostly useless) information about the symbolic link itself, use lstat rather than stat (which returns the same information in the same order). If the operand isn’t a symbolic link, lstat returns the same things that stat would.
my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($filename);
Chapter 13 Directory Operations
Globbing
my @all_files = glob '*'; my @pm_files = glob '*.pm'; my @all_files_including_dot = glob '.* *'; my @all_files = <*>; # exactly the same as my @all_files = glob "*";
my @files =; # a glob my @lines = ; # a filehandle read my @lines = <$fred>; # a filehandle read my $name = 'FRED'; my @files = <$name/*>; # a glob
my $name = 'FRED'; my @lines = <$name>; # an indirect filehandle read of FRED handle my @lines = readline FRED; # read from FRED my @lines = readline $name; # read from FRED
Files Operation
unlink 'slate', 'bedrock', 'lava'; unlink qw(slate bedrock lava); unlink glob '*.o'; rename 'old', 'new'; link 'chicken', 'egg'; mkdir 'fred', 0755; rmdir $temp_dir; chmod 0755, 'fred', 'barney'; chown $user, $group, glob '*.o';
如果用 $file = "~/test.pl" 則會失敗, 需要用到 shell expansion 時, 必須要改用 <> 包起來
$file = <~/test.pl> # Name the file open(INFO, $file) || die "Can't open file $file : $!\n"; # Open the file, print the error message if fail close(INFO); # Close the file
沒有留言:
張貼留言