I'm trying to write a simple logger class (code below) ... when I create more than one object from another Perl file, if I call ->message the text ends up in the correct file, if I call ->finish everything ends up in one file. I guess my mistake is to have created just one filehandle, but I can't spot what I'm doing wrong or where. I'd be grateful if someone can point out where I'm going wrong. Thanks
sub new { my $class_name = shift; my $self = {}; $output_file = shift; $append = shift; $file_handle = undef ; bless ($self,$class_name); bless ($self); return $self; } # opens log file for writing # (no argument) will be called from first call to &message if not call +ed from outside sub start{ my $now = &get_timestamp; print "opening $output_file\n"; if (uc($append)eq'APPEND') { open ($file_handle,">>$output_file") || open ($file_handle,"> +$output_file") || die "Can't open $output_file $!"; }else{ open ($file_handle,">$output_file") || die "Can't open $output +_file $!"; } print $file_handle "Started logging at $now\n"; print $file_handle "Logging to $output_file\n"; print $file_handle "\n"; } #log a message # argument <a status>, <a text message> sub message{ #run start subprocedure if file not already open &start if (!$file_handle); my $status = $_[1]; my $text = $_[2]; print $file_handle "$status : $text\n" ; #$message_number++; } #close log # (no argument) sub finish{ #run start subprocedure if file not already open if (!$file_handle){ &start; }else{ my $now = &get_timestamp; print $file_handle "\n"; print $file_handle "Finished logging at $now\n"; print $file_handle "-------------------------------------\n"; #close ($file_handle); } }

In reply to creating 2 file writing objects by postman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.