I have had a lot of trouble, and since worked out the problem, however i dont know why my solution works.

The module (below) is supposed to open a logfile with a supplied name (logger->new(name=>'logname')then simply append to the logfile with a couple of strings eg: logger->log('START','Started prgy at $time').

The problem is in the log method. if i write the code:
my $fh = $self->{_handle}; print $fh "$location, $message. \n";

it all works and is cool. my question is why cant i use $self->{_handle}directly (rather than $fh) with the print statement? as always any comments on the code are welcome..

most of the style here is copied frim "Object Oriented Perl - Conway".


package Logger; $VERSION = 1.00; use strict; use vars qw( $AUTOLOAD ); use Carp; { #Encapsulated class data my %_log_attr = # DEFAULT ACCESSIBILITY ( _name => ['DEFAULT', 'read/write'], _handle => [undef, 'read'] ); #is a specirfied object attribute accessible in a given mode sub _accessible { my ($self, $attr, $mode) = @_; $_log_attr{$attr}[1] =~ /$mode/ } # Classwide default value for a specified object attributes sub _default_for { my ($self, $attr) = @_; $_log_attr{$attr}[2]; } # list of names of all specified object attributes sub _standard_keys { keys %_log_attr; } } sub new { my ($caller, %arg) = @_; my $caller_is_obj = ref($caller); my $class = $caller_is_obj || $caller; my $self = bless {}, $class; foreach my $attrname ($self->_standard_keys() ) { my ($argname) = ($attrname =~ /^_(.*)/); if (exists $arg{$argname}) { $self->{$attrname} = $arg{$argname} ; } elsif ($caller_is_obj) { $self->{$attrname} = $caller->{$argname}; } else { $self->{$attrname} = $self->_default_for($attrname) } } $self->{_handle}=&prepare_file($self); return $self; } sub prepare_file { my $self = shift; if ($self->{_name}) { my @time=localtime; ++$time[4]; if (length($time[3])eq 1 ) {$time[3]='0'.$time[3]}; if (length($time[4])eq 1 ) {$time[4]='0'.$time[4]}; $time[5]=$time[5]+1900; my $name = '>>'.$self->{_name}.".".$time[3].$time[4].$time[5]. +'.log'; local*LOGFILE; open(LOGFILE, $name) || undef; return *LOGFILE; } } sub log { use Data::Dumper; #print Dumper(@_); my ($self, $location, $message) = @_; if ($self->{_handle}) { my $fh = $self->{_handle}; print $fh "$location, $message. \n"; } else { print "Must call logger->new(name=>'<logfile>') before writing + log"; } } 1

In reply to dereferencing file handles by Ryszard

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.