簡體   English   中英

檢查調用 Perl open 函數的結果的最佳方法是什么?

[英]What's the best way to check the result of calling the Perl open function?

我在 perl 中有運行以下代碼的代碼:

sub runMyCommand{
  my $filename ="testing.txt"
  foreach my $arg (@_) {
    my $path = catfile($arg, $filename);
    if(open my $fh, ">" , $path){
      print $fh "my path is :\$pah\n"
      close $fh;
    }else{
      ....
    }
  }
} 

runMyCommand("/opt/");

出於某種原因,這一直到其他部分。 即使我可以去 /opt/ 並寫一個文件。 我使用 open 的方式有問題嗎?

謝謝你,

在 Perl 中使用 open 函數的更好方法是遵循這樣的模式。

open my $fh, ">", $path
    or die "failed to open $path: $!"

通過這種方式,您會收到一個致命錯誤,其中包含操作系統錯誤變量 $! 提供的描述性消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM