Perl错误子字符串

时间:2012-08-27 23:44:35

标签: perl file substr

在工作中,我们会收到一个大文本文件,该文件由第三方宽度分隔。我想使用perl脚本只获取我们关心的文件片段,因为这些片段最终会被放入数据库中。我对perl很新,就在今天开始。

来到我们的文件有客户信息,我需要选择它的某些部分并最终将它们加载到数据库中,即名称,日期,信息。我正在尝试使用此脚本,但我收到错误“不能使用字符串(秒)作为符号引用,而”严格引用“正在使用”

如果我采取严格的关闭,我猜这不是一个好习惯然后我得到错误“打印<>未打开的文件句柄(秒)”

test.txt的内容是“First Second Third Fourth”,所以我看到substr正在得到我想要的东西。我做错了什么,我是朝着正确的方向前进的吗?

use strict;
use warnings;


open FILE, "<", "test.txt" or die $!;

#get the contents of the file
#my @a_Lines = <FILE>;
my $entireFile = <FILE>;

#close it I don't need it any more
close FILE or die "Cannot close the file because - $!";


#print @a_Lines ;
print $entireFile;
print "\n";

my $firstName = substr $entireFile, 6, 6;
print $firstName

print "\n";

1 个答案:

答案 0 :(得分:6)

你错过了一个分号;改变这个:

print $firstName

到此:

print $firstName;

(错误消息如此奇怪的原因是,由于缺少分号,Perl误解了你想要$firstName的解释方式。它认为你想要$firstName的内容被视为文件句柄的名称。)