in reply to Re: Using an attribute function from a .pm file.
in thread Using an attribute function from a .pm file.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Using an attribute function from a .pm file.
by shmem (Chancellor) on Aug 12, 2007 at 17:39 UTC | |
Well I commented out the use strict.What for? (Though I would like to fix it and use strict. But let us move past this for now.) No we won't, because the stricture warning gives you a clue about what's happening. You are using something as a hash ref which isn't, at line 42, which reads
See the hash ref in there, which isn't? You are dying at that line with an unappropriate message. Stick the filename you are trying to open in there, and $! to get the reason why something might go wrong. Rewriting your sub log_message
I get
You are invoking log_message as a function inside your handler sub created in sub _log
and perl is just doing what you tell it to do... --shmem
| [reply] [d/l] [select] |
by shobhit (Sexton) on Aug 12, 2007 at 18:26 UTC | |
Thats not a good thing to do, and not an excuse to do it at all. So now the problem is that in sub log_message my $self actually contains the string "Entering sub main::do_something\n". I believe that my should put the class name in $self. I am referring to what I learnt here: A simple example OO script for total beginners. Maybe my understanding of the concept is wrong. Now how do I get the hash value of $self->{logfile} from sub log_message? | [reply] [d/l] [select] |
by shmem (Chancellor) on Aug 12, 2007 at 19:08 UTC | |
I believe that myshould 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
to store that into a lexical hash for the invoking package, in the import() subroutine of LogMessages :
Then you get the logfile stored for the using package from the lexical hash:
and then you have the logfile as first argument in the logging sub:
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
| [reply] [d/l] [select] |
by shobhit (Sexton) on Aug 13, 2007 at 09:17 UTC | |