[英]Why does my perl script behave differently if I run it from the icon vs running it from the command line?
[英]Why does Perl's “system ('some command')” behave differently from c:\> some command?
在我的(cmd.exe)中,當前工作目錄是名為libre-office-document.odt的文件 。 我可以使用start
命令打開此文檔而不會出現問題:
c:\path\to\> start libre-office-document.odt
但是,如果我使用以下簡單的Perl腳本
use warnings;
use strict;
print system("start libre-office-document.odt")
並在同一目錄中執行它,我得到以下消息框:
LibreOffice 4.4 - 致命錯誤
該應用程序無法啟動。
用戶安裝無法完成。
顯然,使用Perl的system
命令時,某些行為會有所不同。 我無法確定這是什么以及如何使用system
命令打開文檔。
使用反引號來捕獲輸出。 您可以檢查與assoc和ftype的文件關聯,而不是硬編碼,或者嘗試這樣做:
use strict;
use warnings;
my $assoc = `assoc .odt`;
# .odt=LibreOffice.WriterDocument.1
chomp $assoc;
$assoc =~ s/.*=//g;
my $ftype = `ftype $assoc`;
# LibreOffice.WriterDocument.1="C:\Program Files\LibreOffice\program\soffice.exe" -o "%1"
chomp $ftype;
$ftype =~ /.*=("[^"]*")/;
$ftype = $1;
# "C:\Program Files\LibreOffice\program\soffice.exe" libre-office-document.odt
my $cmd = "$ftype libre-office-document.odt";
system($cmd);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.