Perl 中的後置反引號運算子


使用後置反引號運算子是最簡單的 Perl 程式中執行任何 Unix 命令的方法。只需將命令放在後置反引號運算子內,這將導致執行命令並返回可儲存其結果,如下所示 −

#!/usr/bin/perl
@files = `ls -l`;
foreach $file (@files) {
   print $file;
}
1;

當執行以上程式碼時,它會列出當前目錄中所有可用檔案和目錄 −

drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14
drwxr-xr-x 4 root root 4096 Sep 13 07:54 android
-rw-r--r-- 1 root root 574 Sep 17 15:16 index.htm
drwxr-xr-x 3 544 401 4096 Jul 6 16:49 MIME-Lite-3.01
-rw-r--r-- 1 root root 71 Sep 17 15:16 test.pl
drwx------ 2 root root 4096 Sep 17 15:11 vAtrJdy

更新日期:2019-12-02

185 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.