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");

In reply to Passing a variable recursively by timtowtdi

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.