CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
using global variable across "required" files

 



gdan2000
Novice

Aug 20, 2011, 11:52 PM

Post #1 of 4 (2324 views)
using global variable across "required" files Can't Post

Hi,

#main
require "test1.pl"
require "test2.pl"

test1.pl and test2.pl have all kind of functions and each one should be able to print to the log file (the SAME log destination,rotation and size, log format and so on...)

What is correct approach to do that?
For example I thought about to pass hash argument with log parameters from the main script to every called function, but it sounds to me not so nice a way to do it.


FishMonger
Veteran / Moderator

Aug 21, 2011, 7:23 AM

Post #2 of 4 (2315 views)
Re: [gdan2000] using global variable across "required" files [In reply to] Can't Post

The correct and best approach would be to not use global vars, but instead use a module designed for this purpose.

http://search.cpan.org/~mschilli/Log-Log4perl-1.33/lib/Log/Log4perl.pm


rovf
Veteran

Aug 22, 2011, 12:56 AM

Post #3 of 4 (2259 views)
Re: [gdan2000] using global variable across "required" files [In reply to] Can't Post

I think the correct solution depends on to what extent test1.pl and test2.pl should keep their possibility to be "independent" on logging.

If it is a "hard requirement" that they should use the same logger, having a logging module which is used by test1 and test2, is certainly the best solution. It can be Log4Perl, as Fishmonger has suggested, or it can be a module you write yourself. I personally use Log4Perl and am quite happy with it, though it is pretty complex and takes some time to be understood.

OTOH, if you want to keep the option available, that one day test1.pl and test2.pl, though being both part of your application, maybe again use different logging mechanisms, I suggest to turn them into modules (if your application logic allows this), and pass the necessary information about the logfiles as arguments.


FishMonger
Veteran / Moderator

Aug 22, 2011, 7:14 AM

Post #4 of 4 (2254 views)
Re: [rovf] using global variable across "required" files [In reply to] Can't Post

By "independent" logging, do you mean different log files and different log format? If so, then Log4Perl would be the best choice and the logging for each script could be dynamically controlled.

Using Log4Perl can get complex, but that doesn't mean that it needs to be complex.

Precisely what do you mean by "different logging mechanisms" and how does passing the necessary information about the logfiles as arguments tie in with different logging mechanisms?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives