2014-10-03 31 views
1

我應該在寫入模式下打開一個新文件並將數據寫入它。 請讓我建議,如何在安全模式下打開一個新文件?如何安全地在寫模式下創建新文件?

與下面的代碼我得到錯誤「文件不存在或無法讀取:/root/usr/data.xml;

my $new_file = "/root/usr/data.xml"; 
my $dom = $parser->load_xml(...); 
dom->toFile($new_file); 
+0

如何我是否首先創建新文件並在寫入模式下打開? – Giri 2014-10-03 06:21:39

回答

1

試試這個代碼:

use strict; 
use warnings; 
use autodie; 

my $new_file = "/root/usr/data.xml"; 
unless(-e $new_file) { 
    #Create the file if it doesn't exist 
    open my $fc, ">", $new_file ; 
    close $fc; 
} 

# Work with the file 
open my $fh, "<", $new_file; 
while(my $line = <$fh>) { 
    #... 
} 
close $fh; 
相關問題