Kevin
Kevin

Reputation: 465

Perl error substring

At work we are sent a large text file that is width delimited from a third party. I want to use a perl script to only get pieces of the file we care about because those pieces will eventually be put into a database. I'm very new to perl, just started today.

The file that comes to us has customer information and I need to select certain parts of it and eventually load those up into a database, i.e. names,dates, info. I'm trying to use this script but I get an error "Can't use string (Second) as a symbol ref while "strict refs" is in use"

if I take the strict off, which I'm guessing is not a good practice then i get the error " print<> on unopened filehandle (Second)"

contents of test.txt is "First Second Third Fourth" so i see that the substr is getting what I'm wanting it to get. What am I doing wrong and am I going in the right direction?

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";

Upvotes: 0

Views: 182

Answers (1)

ruakh
ruakh

Reputation: 183321

You're missing a semicolon; change this:

print $firstName

to this:

print $firstName;

(The reason that the error-message is so strange is that, because of the missing semicolon, Perl is misunderstanding how you want $firstName to be interpreted. It thinks you want the contents of $firstName to be treated as the name of a filehandle.)

Upvotes: 5

Related Questions