timtowtdi has asked for the wisdom of the Perl Monks concerning the following question:
First, I know there has been written a lot about this issue, but I still have some issues with it. (Basically: I just don't understand :) )
I think I can explain my question the best with the factual example. What I want is setting a log-level in the main program, and let the printing be done by a module.
I've written my first Object-Oriented module, named: PrintToScreen.pm
#!/usr/bin/perl package PrintToScreen; use strict; my $version = "0.1"; ############################### # Verbosity # # 0 = Error # 1 = Normal # 2 = Verbose # 3 = Debug # ############################### sub new { my $class = shift; my $self = { _usersetloglevel => shift, }; my $usersetloglevel = $self->{_usersetloglevel}; if ($usersetloglevel >= 3) { print "$class $version started with loglevel $usersetloglevel\ +n"; } bless $self, $class; return $self; } sub Print { my ( $self, $msglevel, $message ) = @_; if ($msglevel <= $self->{_usersetloglevel}) { print "$message"; } } 1;
And some perl-program to call that module and print, depending of course, on the log-level.
setverbosity.pl#!/usr/bin/perl use strict; use warnings; use PrintToScreen; use ConnectMySQL; # just declaration my $loglevel = 3 ; my $pts_obj = new PrintToScreen( $loglevel ); $pts_obj->Print( 1, "Message with level 1! :-)\n"); $pts_obj->Print( 2, "Message with level 2! :-)\n"); $pts_obj->Print( 3, "Message with level 3! :-)\n");
So far it works, but here's my issue:
I also use a module, named ConnectMySQL.pm, and I would like to pass the value of the log-level to that ConnectMySQL-module. That ConnectMySQL-module also contains 'use PrintToScreen'. So the log-level should be the same as the main program that's calling it.
Of course, the main idea behind al this that I can set the verbosity (/loglevel) while programming on 3, and when everything is finished, set it to 1, so it behaves normal. (including the used modules recursively)
I am afraid that my explanation stinks, but I still think you'll get my point.
For example the ConnectMySQL.pm looks like this: (just imagine the mysql-connect-code)#!/usr/bin/perl package ConnectMySQL use strict; use PrintToScreen; my $pts_obj; $pts_obj->Print( 3, "About to connect to mysql-database... "); #connect blablabla $pts_obj->Print( 3, "Done!\n");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing a variable recursively
by tobyink (Canon) on Nov 04, 2012 at 21:28 UTC | |
by timtowtdi (Sexton) on Nov 05, 2012 at 14:13 UTC | |
|
Re: Passing a variable recursively
by rjt (Curate) on Nov 04, 2012 at 23:19 UTC | |
by timtowtdi (Sexton) on Nov 05, 2012 at 14:20 UTC | |
|
Re: Passing a variable recursively
by marinersk (Priest) on Nov 06, 2012 at 07:36 UTC | |
by tobyink (Canon) on Nov 06, 2012 at 09:30 UTC |