I believe that my
41 $self=shift
should put the class name in $self.

No - shift (defaults to shift @_) removes the first argument to the subroutine from its argument list (that's @_) and returns it; that's what you are storing into the variable $self. No voodoo here. Saying $self and shift doesn't make an object. The first argument to that subroutine is the string "Entering sub $pkg\:\:$name\n".

Now how do I get the hash value of $self->{logfile} from sub log_message?
Were $self a hash actually, and had it a key 'logfile' in it, you would get it from there.

A previous ocurrence of that key is in your sub new in LogMessages.pm, but you don't make use of that method. It doesn't make much sense anyways, since all you want is the logfile name, not an object to pass around. So I'd pass that file name when use-ing LogMessages

use LogMessages qw(/path/to/logfile);

to store that into a lexical hash for the invoking package, in the import() subroutine of LogMessages :

my %logfile; sub import { my $class = shift; $logfile{ +caller} = @_ ? shift : "/tmp/logfile.$$"; };

Then you get the logfile stored for the using package from the lexical hash:

sub _log : ATTR(CODE) { my ($pkg, $sym, $code) = @_; if ( DEBUG ) { my $name = *{ $sym }{NAME}; no warnings 'redefine'; # Turn off symbol redefinitions within + the block. *{ $sym } = sub { log_message ($logfile{$pkg}, "Entering sub $pkg\:\:$name\n +"); my @ret = $code->( @_ ); log_message ($logfile{$pkg}, "Leaving sub $pkg\:\:$name\n" +); return @ret; }; } }

and then you have the logfile as first argument in the logging sub:

sub log_message { my $logfile = shift; open my $LOGFILE, '>>', $logfile or die "Cannot open logfile '$logfile': $!"; my $time = getLogTime(); print $LOGFILE $time, " "; for (@_){ print $LOGFILE @_; } }

Note three argument open and lexical filehandle. Don't forget to put a 1; at the end of your LogMessages.pm ...

You are using @EXPORT and are stuffing Exporter into @ISA, but there's nowhere you do use Exporter in LogMessages. You don't need it, either, since you have nothing to export.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re^5: Using an attribute function from a .pm file. by shmem
in thread Using an attribute function from a .pm file. by shobhit

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.