Perl的反复无法在Windows上运行,但在Linux上运行良好

时间:2015-01-02 06:23:10

标签: windows perl ubuntu

我有简单的脚本perlSample.pl,只打印日期和时间:

my $cmd = 'perl -e \'my $datestring = localtime( time );print $datestring\'';
my $line = `$cmd 2>&1`;
my $ret = $?;
print $line;

这个脚本在Ubuntu中运行正常,但是当我在Windows Xp上运行时它给我错误:

Can't find string terminator "'" anywhere before EOF at -e line 1.

在Windows XP上,我有ActivePerl(v5.20.1)和Ubuntu(v5.20.1)相同的版本。错误在哪里?

4 个答案:

答案 0 :(得分:1)

您想要实现的内容可以更加简单和便携,而无需调用perl解释器的外部实例,

my $line = localtime( time );
print $line;

但是如果你因某些原因坚持下去,你必须在win32下使用双引号,perl -e ".."

my $cmd = 'perl -e "my $datestring = localtime( time );print $datestring"';
my $line = `$cmd 2>&1`;
my $ret = $?;
print $line;

答案 1 :(得分:0)

我找到了     我的$ cmd =" perl -e print(当地时间(时间))&#34 ;; 至少在Windows 7上运行良好 - 不再使用WindowsXP。

这使外部化命令可以在Windows和Linux中使用。

答案 2 :(得分:0)

my $cmd = 'perl -e "my $datestring = localtime( time );print $datestring"'; my $line = $ cmd 2>& 1 ; my $ret = $?; print "$line $ret";

  

单引号在Windows命令行中不起作用你必须使用双引号才能在windows中获得相同的结果

答案 3 :(得分:0)

您应该使用module处理为您引用参数。

use IPC::Cmd qw' run ';

# prints to STDERR as an example
my @cmd = ('perl', '-e', 'print STDERR scalar localtime');

my $lines; # the output buffer

# buffer gets both STDOUT and STDERR
run( command => \@cmd, buffer => \$lines );

print $lines, "\n";

它具有Perl附带的好处,并且它会为您的平台适当地引用该命令。


在Perl6中执行此操作的方式略有不同,因为它不会与IPC::Cmd一起提供,相反,您很可能会使用内置的Proc::Async模块。

#! /usr/bin/env perl6

use v6; # provides a good error message on Perl 5.8 and newer

my $process = Proc::Async.new('perl6', '-e', '$*ERR.say: DateTime.now');

my @parts;

$process.stdout.tap: { @parts.push('STDOUT' => $_) unless /^ \s* $/ } #;
$process.stderr.tap( { @parts.push('STDERR' => $_) unless /^ \s* $/ } );
# you can "tap" the stderr and stdout Supplys multiple times if you want

{
  my $promise = $process.start;
  # can do other things here, while we wait

  # halt this program until the other one finishes
  await $promise;
}

.say for @parts;
STDERR => 2015-01-26T10:05:42-0600

正如您所看到的,使用Proc::Async模块需要更多代码,但它通过更灵活来弥补它。