O
Olivier Laurent
Hi there,
I'm playing with perl to understand how I could make modules.
Here is my "first module" ...And...It doesn't work.
This module should parse a CSV file an extract the appropriate columns
(it should return an array).
I call it with:
my $test= parse_file->new(FILE=>"./log/1fr.txt",AREA=>6);
my $arrayref=$test->getdata();
But it fails. I receive an error "could not open SCALAR(XXXX)";
it seems that FILE is transformed into a hexadecimal code ?
What is my mistake?
package parse_file;
use 5.001;
use strict;
# use warnings;
sub new {
my $self = {};
$self->{'FILE'} = undef;
$self->{AREA} = undef;
bless {$self};
}
sub getdata
{
my $self;
my $file = $self->{FILE};
my $area = $self->{AREA};
my @data;
my @return_data;
open(MYFILE,'$file')|| die "cannot open $file: $!";
while(defined($a=<MYFILE>))
{
my @data=split(/\,/,$a);
push(@return_data,$data[$area]);
}
close(MYFILE);
$self->{'var'} = \@return_data;
# $self->{'var'}= \$file;
}
1;
Thank you!
Olivier
I'm playing with perl to understand how I could make modules.
Here is my "first module" ...And...It doesn't work.
This module should parse a CSV file an extract the appropriate columns
(it should return an array).
I call it with:
my $test= parse_file->new(FILE=>"./log/1fr.txt",AREA=>6);
my $arrayref=$test->getdata();
But it fails. I receive an error "could not open SCALAR(XXXX)";
it seems that FILE is transformed into a hexadecimal code ?
What is my mistake?
package parse_file;
use 5.001;
use strict;
# use warnings;
sub new {
my $self = {};
$self->{'FILE'} = undef;
$self->{AREA} = undef;
bless {$self};
}
sub getdata
{
my $self;
my $file = $self->{FILE};
my $area = $self->{AREA};
my @data;
my @return_data;
open(MYFILE,'$file')|| die "cannot open $file: $!";
while(defined($a=<MYFILE>))
{
my @data=split(/\,/,$a);
push(@return_data,$data[$area]);
}
close(MYFILE);
$self->{'var'} = \@return_data;
# $self->{'var'}= \$file;
}
1;
Thank you!
Olivier