【问题标题】:Backticks in directory query generate problems with pipe目录查询中的反引号会产生管道问题
【发布时间】:2013-06-07 05:21:03
【问题描述】:

我正在尝试用目录 $dir 中的文件名列表填充 Perl 数组,并且我想排除目录。我的代码是:

my @lsArray = `ls -p $dir | grep -v '/$'`;

但它会产生这个错误:

sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file

我似乎找不到正确执行此操作的引号组合。谢谢....

【问题讨论】:

标签: perl grep pipe ls


【解决方案1】:

为什么你要使用反引号来表示 perl 可以完全独立完成的事情?

my @files = grep !-d, <$dir/*>;

顺便说一句,您的错误是 $' 是一个被插值的 perl 预定义变量(后匹配),因此缺少关闭 '

如果你用过

use strict;
use warnings;

Perl 会告诉你问题出在哪里:

Use of uninitialized value $' in quoted execution (``, qx) at ...

始终使用这两个 pragma:错误不会因为您不了解它们而变得更难处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多