I'd be grateful if someone can point out where I'm going wrong
Brother
postman, since you have displayed interest in our (my) advice, let me suggest to you that it's always better to use modules that are already on the CPAN other than re-invent your own wheel. The advantage of this approach is a plenty. First, CPAN modules are generally well (or sufficiantly) tested. Second, you don't have to do any work re-implementing the tool/functionality.
As for specific module suggestions, check out these ones:
- Log::Agent
The module provides an abstract layer for logging and tracing.
- Log::Channel
Allows for code to specify channels for delivery of logging messages, and for users of the code to control the delivery and formatting of the messages.
- Log::Simple
This is a basic run-time logger. My favourite ;).
UPDATE:
Also, as far as your class code goes, you aren't doing proper OO implementation. The
$filehandle should be stored 'inside' your object. So, your constructor should look as follows:
sub new {
my $class_name = shift;
my $self = {};
$self->{output_file} = shift;
$self->{append} = shift;
$self->{file_handle} = undef ;
bless ($self,$class_name);
# bless ($self); # AHA! How could I have missed that one!
+Thanks go to [bronto] for pointing that out. No need to bless the thi
+ng twice.. one time is enough already! ;>
return $self;
}
Further, the rest of the methods in your modules aren't even true 'member' methods as they don't make use of the object 'reference'. In Perl, you've got to link a method to an object as so:
sub start {
my $self = shift; # Get 'reference' to the class instance (aka obje
+ct)!
# use this reference to retrieve object specific attributes!
open($self->{filehandle}, " . . . ");
# . . . Rest of your code . . .
}
Update: bronto thanks for pointing that out. However, the last portion of your statement is invalid:
but I don't see the need to bless $self twice, an error that you inherited from vladb's code
I didn't make any errors and therefore noone could have 'inherited' any from me. I simply copied and pasted the original poster's code and overlooked that he (the original author, not I) called the 'bless' method twice.
_____________________
# Under Construction
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.