1、讀取某檔案,如果該檔案不存在,則報錯,並提示出錯原因
open (db, "/home/ellie/myfile") or die "can't open file: $!\n";
2、讀寫檔案的方法:
open(fh, "); # opens "filename" for reading.讀# the <; symbol is optional.
open(fh, ">filename"); # opens "filename" for writing.寫
# creates or truncates file.
open(fh, "+); # opens "filename" for read, then write.寫讀後寫
open(fh, "+>filename"); # opens "filename" for write, then read.先寫後讀
close(fh);
3、開啟並列印該檔案
open(fh, ") or die "can't open file: $!\n"
;while()
4、檔案屬性
#!/usr/bin/perlmy $file="d:/readtest.txt";
# is it readble, writeable, and executable?
print "file is readable, writeable, and executable\n"
if -r $file and -w _ and -x _;
# when was it last modified?
print "file was last modified ",-m $file, " days ago.\n";
#若為目錄則列印
print "file is a directory.\n "
if -d $file; # is it a directory?
由於此檔案實際存在,並且是剛建不久,但只是普通的文字檔案,因此最後的結果為
file was last modified 0.0239930555555556 days ago.
若**:print "file is readable, writeable, and executable\n"
if -r $file and -w _ and -x _;
改為:print "file is readable, writeable, and executable\n"
if -r $file and -w _ ;
最後的結果則為:
file is readable, writeable, and executable
file was last modified 0.0251851851851852 days ago.
-w _為-w $file的簡寫。
perl 檔案操作
perl中檔案操 基礎在perl中,提供了三種檔案控制代碼 stdin,stdout,stderr.它們可以由父程序建立的檔案或者裝置會自動開啟.一般使用open函式來建立檔案控制代碼.open函式的語法是唯讀模式時為 open filehandle,somename 其中filehandle是檔案...
perl 檔案操作總結
一 開啟 關閉檔案 open的返回值用來確定開啟檔案的操作是否成功,當其成功時返回非零值,失敗時返回零,因此可以如下判斷 if open myfile,myfile 當檔案操作完畢後,用close myfile 關閉檔案。讀 open 檔案控制代碼,檔名 open 檔案控制代碼,檔名 前提檔案必須已...
perl的檔案操作 1
假如有乙個從web上拷貝下來的檔案 01 usr bin perl w 02 03 use tk 04 05 tk strictmotif 1 06 07 main mainwindow new 08 09 button1 main button text exit 10 command exit ...