本文介绍了$/是否有可能支持正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用perlvar:

... $/ 的值是一个字符串,而不是一个正则表达式.awk 必须更好.:-)

不难想到这种特性会在哪些情况下有用 - 解析具有可变长度记录的文件是我多次遇到的经典用例.

It is not difficult to think of situations where such a feature would be useful - parsing files with variable-length records is a classic use case which I encounter many times.

到目前为止,我从来没有遇到过将整个文件加载到内存中并执行以下操作的问题:

So far I have never had trouble loading the entire file into memory and do a :

my @records = split /my_regex/, <> ;

但由于显而易见的原因,这种技术不能用于可用内存不足的情况.事实上,很多时候不需要同时存储所有记录.

but for obvious reasons this technique cannot be used in situations where available memory is inadequate. In fact, many a time there is no need for all records to be stored at the same time.

这让我回到了 $/.

我觉得奇怪的是,该语言没有为 $/ 提供正则表达式支持.这是故意设计的吗?是根本不可能实施吗?在没有什么漂亮的功能的情况下,还有哪些其他解决方法可以被视为最佳实践?

I find it odd that the language has not provisioned regex support for $/. Was this done by design? Is it simply impossible to implement? What other workarounds exist that can be considered as best practices in the absence of what would be a nifty feature?

推荐答案

即使尝试也没有多大意义.很多时候,如果不阅读它的末尾,您将无法判断是否已到达该行的末尾.这在交互式情况下可能非常糟糕.

It doesn't make much sense to even try. Far too often, you wouldn't be able to tell if you've reached the end of the line without reading past its end. That could be very bad in interactive situations.

例如,假设您有以下程序:

For example, let's say you have the following program:

local $/ = qr/\n|\r\n?/;  # Handle Windows, Unix and old MacOS line endings.
while (1) {
   print "Please enter a command: ";
   my $cmd = <>;
   $cmd =~ s{$/\z}{};
   process($cmd);
}

看起来很简单,对吧?事实上,支持 qr/\n|\r\n?/ 可能是这个请求的首要原因.好吧,即使是那个简单的代码也存在严重缺陷.假设我使用 MacOS 行尾 (CR, ^M, \r)

Looks pretty straightforward, right? In fact, supporting qr/\n|\r\n?/ is probably the number one reason for this request. Well, even that simple code is severely flawed. Let's say I use MacOS line endings (CR, ^M, \r)

 $ processor
 Please enter a command: foo^M
 [hangs]

程序挂起是因为它无法判断我给它的是 MacOS 行尾 (CR, ^M, \r) 还是 Windows 行尾 (CRLF, ^M^J, \r\n) 直到另一个字符输入.

The program hangs because it can't tell whether I gave it a MacOS line ending (CR, ^M, \r) or a Windows line ending (CRLF, ^M^J, \r\n) until another character is typed.

我必须输入第二个命令来处理第一个,第三个命令来处理第二个,依此类推.这毫无意义.

I'd have to enter a second command to process the first, a third command to process the second, etc. It just makes no sense.

这篇关于$/是否有可能支持正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 18:48