open (FILE, "file.txt");
close(FILE);
open(handle名,"文件名");
2)
open (FILE, "file.txt") or die("Error");
print ;
close(FILE);
or die("Error")如果文件不存在,显示Error message。
3)
open (FILE, "file.txt");
@array = ;
close(FILE);
print @array;
foreach $line (@array)
{
chomp($line);
$line = uc($line);
print "$line";
}
chomp 去掉结尾的\n;
chop 是去掉最后一个字符;
uc Upper Case;
4)Write to Text file
open(FILE, ">file.txt");
flock(FILE,2);
print FILE "Hello\n";
close(FILE);
">" 是准备写入的开始
flock 是将文件锁上,以防止其他人操作同一个文件
5)append to text file
open(FILE, ">>file.txt");
print FILE "Hello\n";
close(FILE);
">>" 是准备添加到文件
"<>" is operator directs Perl to read a line from the file handle specified within the angled brackets.
6)Rename file
rename "file.txt", "file2.txt";
7)Copy file (use perl module)
use File::Copy;
copy("file.txt", "copied.txt");
8)delete file
unlink "file.txt";
9)
$file = "copied.txt";
$size = (stat $file)[7];
stat function不同的参数用于不用的作用,比如7代表的是文件的字节数。
10)Open Directory
opendir(DIR, 'directory1');
@files = readdir(DIR);
closedir(DIR);
readdir 读取文件夹里有什么文件和文件夹。
11)Change Directory
chdir 'directory2'; ##开始使用另一个文件夹
mkdir 'newdirectory'; ##文件夹改名
chdir '..'; ##返回上一级文件夹
12)Ddelete Directory
rmdir 'newdirectory';
13)Glob function for Directory
@files = glob('directory1/*');
print "@files\n";
结果打印出包括 文件夹名/文件名 directory1/file1.txt
@files = glob('directory1/*.txt'); ##only .txt 文件
push (@files2, glob('directory1/*.bmp");
print "@files2\n";
push的作用是是上边直接付给@files一样的结果
直接print出来用join
print join("\n", glob('directory1/*.cgi'));
or
while ()
{
print "$_\n";
}
No comments:
Post a Comment