perl - 从 perl 中的 Windows 程序读取数据时,打开时出现无效参数错误

标签 perl runtime-error

我是一名中级用户,已被迫使用通过获取许可证管理器状态报告来监视程序使用情况的例程。 我在运行 Windows Server 2019 Standard V 1809 的服务器上使用 perl

我目前在使用一个新程序(基于有效的程序)时遇到问题,该程序在运行获取状态的行时会产生“无效参数”错误。

程序中我遇到问题的行是:

# $command1 = "c:\\Cadence\\LicenseManager\\lmutil.exe lmstat -a -c `c:\\Cadence\\LicenseManager\\license.dat |";
$command1 = '"C:\Program Files\InnovMetric\PolyWorks MS License Server 2022\bin\lmutil" lmstat -a -c "C:\Program Files\InnovMetric\PolyWorks MS License Server 2022\innovmet.lic"';

print "Read license status - >$command1<\n\n";
open(STATUS, $command1) || die "Couldn't run $command1: $!\n";

$command1 赋值的注释版本适用于以前的程序。 未注释的版本生成 "C:\Program Files\InnovMetric\PolyWorks MS License Server 2022\bin\lmutil"lmstat -a -c "C:\Program Files\InnovMetric\PolyWorks MS License Server 2022\innovmet.lic" 它可以作为命令提示符下的命令完美运行。 我看到的最大区别是我必须使用 ' 以便我可以获得处理目录名称中的空格所需的引号。我没有指定目录名称。 未注释的版本在命令中没有管道。我想确定这不是问题的原因,并且它无法从命令行运行。

我从这部分代码得到的输出是:

Read license status - >"C:/Program Files/InnovMetric/PolyWorks MS License Server 2022/bin/lmutil" lmstat -a -c "C:/Program Files/InnovMetric/PolyWorks MS License Server 2022/innovmet.lic"<

Couldn't run "C:/Program Files/InnovMetric/PolyWorks MS License Server 2022/bin/lmutil" lmstat -a -c "C:/Program Files/InnovMetric/PolyWorks MS License Server 2022/innovmet.lic": Invalid argument

有人知道为什么 perl 不喜欢我要求它运行的命令行吗?

最佳答案

修复了原始问题中要编译的代码后,我在运行时得到了这个

>perl.exe try.pl
Read license status - >"C:\Program Files\InnovMetric\PolyWorks MS License Server 2022\bin\lmutil" lmstat -a -c "C:\Program Files\InnovMetric\PolyWorks MS License Server 2022\innovmet.lic"<

Couldn't run "C:\Program Files\InnovMetric\PolyWorks MS License Server 2022\bin\lmutil" lmstat -a -c "C:\Program Files\InnovMetric\PolyWorks MS License Server 2022\innovmet.lic": Invalid argument

这与您所拥有的相符。正如 @user20284150 已经提到的,传递open一个不带尾部|的文件名意味着它将尝试打开文件“C:\Program Files\InnovMetric\PolyWorks MS 许可证服务器 2022\bin\lmutil"lmstat -a -c "C:\Program Files\InnovMetric\PolyWorks MS 许可证服务器 2022\innovmet.lic"。该文件不存在,因此出现错误。不知道为什么它会给出无用的错误 - 本来希望它说没有这样的文件或目录

为了显示区别,这里有一个快速演示。首先执行与您所做的相同的操作,然后运行 ​​open 而不带尾随 |

use strict ;
use warnings;

# Not no trailing "|" in the command
my $command1 = 'perl -v';

print "Read license status - >$command1<\n\n";
open(STATUS, $command1) || die "Couldn't run '$command1': $!\n";

while(<STATUS>)
{
    print "GOT --  $_";
}

运行给出

>perl.exe try.pl
Read license status - >perl -v<

Couldn't run 'perl -v': No such file or directory

这给出了一个有用的错误

现在是相同的代码,但包含尾部 |

use strict ;
use warnings;

# note trailing "|" this time
my $command1 = 'perl -v |';

print "Read license status - >$command1<\n\n";
open(STATUS, $command1) || die "Couldn't run '$command1': $!\n";

while(<STATUS>)
{
    print "GOT --  $_";
}

给出了这个输出

>perl.exe try.pl
Read license status - >perl -v |<

GOT --  
GOT --  This is perl 5, version 32, subversion 1 (v5.32.1) built for MSWin32-x64-multi-thread
GOT --
GOT --  Copyright 1987-2021, Larry Wall
GOT --
GOT --  Perl may be copied only under the terms of either the Artistic License or the
GOT --  GNU General Public License, which may be found in the Perl 5 source kit.
GOT --
GOT --  Complete documentation for Perl, including FAQ lists, should be found on
GOT --  this system using "man perl" or "perldoc perl".  If you have access to the
GOT --  Internet, point your browser at http://www.perl.org/, the Perl Home Page.
GOT --

关于perl - 从 perl 中的 Windows 程序读取数据时,打开时出现无效参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75745364/

相关文章:

perl - 如何在 Perl 中跨模块使用 Log4Perl?

linux - 删除一些文本对的差异

error-handling - 将 .NET Framework 4.5 的 System.IO.FileSystemWatcher 的设置配置为可就错误、容错、健壮、智能等进行交流

excel - 无法绕过 SAP GUI 连接中的运行时错误 619

c - 如何在编译代码中加载附件文件,Chicken Scheme

perl - 从golang运行perl

regex - `?` 在这个 Perl 正则表达式中是什么意思?

linux - 在特定终端中通过 Perl 运行 shell 命令

audio - “[3556] Windows.Media.BackgroundPlayback.exe”已退出,代码为1(0x1)

java - Android 在启动应用程序时出现运行时错误