文件中的特殊字符退出Perl中的while循环 [英] Special character in the file exit the while loop in Perl

查看:238
本文介绍了文件中的特殊字符退出Perl中的while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为.txt文件编写了一个简单的解析器,其中包含以下说明:

I wrote a simple parser for a .txt file with the following instructions:

my $file2 = "test.txt";
open ($process, "<",$file2) or die "couldn't manage to open the file:$file2!";

while (<$process>)

{

...

}

在我要解析的某些文件中,有一个特殊字符,如右箭头(->),并且我没有从文件中粘贴到这里。
每次解析器碰到该字符(->)时,它都会退出文件,直到结束时才进行处理。

In some files that I am trying to parse there is a special character that is like the right arrow (->) and that I don't manage to paste here from the file. Every time the parser hits that character (->), it exits the file without processing it till the end.

有没有一种避免它的方法,并且继续处理文件直到最后吗?

Is there a way to avoid it and continue processing the file till the very end?

我正在使用perl 5.6.1(我不能使用较新的文件),并且需要处理的文件可能包含这些文件

I am using perl 5.6.1 (I cannot use a newer one) and the files that I need to process might have these special characters.

感谢您的帮助。

推荐答案

我不知道认为不是Perl导致了您的问题,但几乎可以肯定的是,这个中间缺失的块中有一些问题。您是否在文件输入的while块中使用eval?这是一个最小的示例,它显示了包含->的流不会引起困难:

I don't think it's perl that's causing your problem, but almost certainly something in that middle missing block. Are you using eval in the while block on the input from the file? This is a minimal example that shows that the stream containing -> doesn't cause difficulties:

#!/usr/bin/env perl
use warnings;
use strict;

while(<DATA>) {
  print "Data[$.]: $_";
}

__DATA__
this is some data
this is also some data
-> this looks fine
foo->dingle also looks fine

这会产生:

$ perl ./foo.pl 
Data[1]: this is some data
Data[2]: this is also some data
Data[3]: -> this looks fine
Data[4]: foo->dingle also looks fine

,perl中的->字符是特殊的:

So, the -> characters in perl are special:


->是中缀取消引用运算符,就像在C和C ++中一样。如果右侧的
是[...],{...}或(...)下标,则左侧的
必须是硬引用或符号引用数组,
a哈希或一个子例程。 (或者从技术上讲,如果
位置是用于分配的数组或哈希
引用,则该位置能够保留硬引用。)请参见perlreftut和perlref。

"-> " is an infix dereference operator, just as it is in C and C++. If the right side is either a [...] , {...} , or a (...) subscript, then the left side must be either a hard or symbolic reference to an array, a hash, or a subroutine respectively. (Or technically speaking, a location capable of holding a hard reference, if it's an array or hash reference being used for assignment.) See perlreftut and perlref.

否则,右侧是方法名称或包含方法名称或子例程引用的简单标量变量
,而
左侧则必须是对象(带祝福的引用) )或类
的名称(即程序包名称)。见perlobj。

Otherwise, the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference, and the left side must be either an object (a blessed reference) or a class name (that is, a package name). See perlobj.

因此,如果我不得不猜测,您肯定是在使用eval尝试解析您的内容,并且无法取消引用操作员的左侧并坠毁。如果您需要进一步的帮助,请提供命令行错误消息或while循环中的代码。

So if I had to guess, you're definitely using eval to try to parse your content and it's failing to dereference the left side of the operator and crashing. Please provide command line error messages or the code in the while loop if you want further assistance.

这篇关于文件中的特殊字符退出Perl中的while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆