Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl OO test file 3

Status
Not open for further replies.

richardko

Programmer
Joined
Jun 20, 2006
Messages
127
Location
US
Hi,
I am trying to create an object for a perl class but am getting stumped. I was wondering if anyone could help me with this. Here is my class definition:
=======================================
package log;
use strict;

#constructor
sub new {
my ($class) = @_;
my $self = {
_filename => undef,
_descriptor => undef
};
bless $self, $class;
return $self;
}

#accessor method for Log filename
sub FileName {
my ($self, $filename)=@_;
$self->{_filename} = $filename if defined ($filename);
return $self->{_filename};
}

#accessor method for Log Descriptor
sub Descriptor{
my ($self, $descriptor) = @_;
$self->{_descriptor} = $descriptor if defined ($descriptor);
return $self->{_descriptor};
}

sub readParse{
my ($self) = @_;
}

sub print{
my ($self) = @_;
#print Log info
printf("Name:%s %s\n\n", $self->filename, $self->descriptor);
}

1;
==============================================
And here's my object creation:
=============================================
#/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use log;

my $khurt = eval { new log(); } or die ($@);
=================================================
And here's the error I am getting:
=================================================
syntax error at logclass.pl line 9, near "new log"
Execution of logclass.pl aborted due to compilation errors (#1)
(F) Probably means you had a syntax error. Common reasons include:

A keyword is misspelled.
A semicolon is missing.
A comma is missing.
An opening or closing parenthesis is missing.
An opening or closing brace is missing.
A closing quote is missing.

======================================================
Sorry for entering questions back to back but I want to have a good working knowledge of perl.
Thanks
ro
 
Got a few issues:

1) "log" is a reserved word in perl.
2) As a general rule, package names should have their first letter capitolized. This would have avoided the reserved word in this isntance. "package Log;"

3) Why are you enclosing it in an eval? Not that there is anything inherantly wrong with that, but it is unnecessary.

That leaves us with:

Code:
use Log;

my $khurt =  new Log();
 
just to address the syntax error:

Code:
my $khurt =  eval { new log(); };
die $@ if $@;

you had a missing semi-colon after the eval block.

- Kevin, perl coder unexceptional!
 
Note the changes in bold.

Code:
#/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use [b]LogUtil[/b];

my $khurt = [b]LogUtil->new()[/b];

# to access a method of the class
[b]$khurt->FileName('my_file.txt');[/b]

M. Brooks
 
Thank you,
Its nice to be learning perl, it feels though I have so much far to go.aargh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top