On Thu, 27 May 2004, Tom Schindl wrote: > Nathanial P. Hendler wrote: > > I'm not sure if this is a style or function question. I'd like to have a > > module called Debug.pm that has a variable in it that if set to '1' by > > anything (main script, or other modules) will print debug messages. > > > > Reading the Orielly "Practical mod_perl" I thought it was clear what I > > needed to do, but I am finding it difficult. The following works as one > > file, but when I break the Debug part into its own file, the value of $D > > just increments with every re-load... > > > > #!/usr/local/bin/perl > > > > use strict; > > use warnings; > > use CGI qw(:standard); > > use Debug; > > > > print header(); > > > > Debug::report('I should not see this line'); > > $Debug::D++; > > Debug::report('I should see this line'); > > $Debug::D++; > > Debug::report('This one too'); > > > > package Debug; > > use strict; > > use warnings; > > > > use vars qw($D); > > $D = 0; > > > > sub report { > > my $string = shift; > > > > print "DEBUG ($D): $string<br>\n" if $D; > > } > > > > 1; > > > > > > Can anyone explain to me why I can't have a global variable when I put > > Debug into its own file? > > > > Well that's mod_perl. It will compile your script only once perl > forked-process|thread (which means it also loads your Debug.pm only > once) hence all global variables hold their value. > > This means the following: > * you should see: > ---------------8<--------------- > I should see this line > This one too > ---------------8<--------------- > when the request is served by new forked/thread process which has not > already loaded your YourModule.pm. > * you should see : > ---------------8<--------------- > I should not see this line > I should see this line > This one too > ---------------8<--------------- > on any next request servered by *exactly this* apache-child. > > What you could do: > * reset $Debug::D directly before print header() > e.g. Debug::reset() > * make an object out of debug: > e.g. $debug = new Debug(), $debug->increment_counter() > * use mod_perls logging capabilities.
Ok, thanks. The reason I haven't just made it an object is that I have several modules, and I'd like them all to be able to call Debug::report() and if the debug value ($D) is set, then it works, without having to pass the debug object to every module. I guess I just don't quite know how to do that. Is there a way to have a module run anycode any time another perl module/script uses it? use Debug; would executed insided Debug.pm IM_BEING_USED { $D = 0; } > Do you want to increase/decrease verbosity or why are you incrementing > $D???? So that a module can do `$D++; blah blah; $D--;` and it won't trounce another module's initiation to turn on debuging. Thanks for your help. Nathan -- Report problems: http://perl.apache.org/bugs/ Mail list info: http://perl.apache.org/maillist/modperl.html List etiquette: http://perl.apache.org/maillist/email-etiquette.html