Hi Monks,
I saw earlier in the Chatterbox this question and an answer, and it catched my attention... The answer was "Yes, but it is commonly considered bad practice. You can easily access global variables of the main program: $main::foo gives access to $foo in the main program"
I tried this and obviously it didn't work, so I searched a little and came on this page : Variable Scoping in Perl: the basics, which I saddly didn't understand as good as I would like to :( basically, I understood that there were 3 was to declare a variable, my our and local, after that I kinda lost myself in the subtilities of each method...
So here is my problem : I'm working on a script which uses Log::Log4perl to log pretty much everything I do. I'm calling a homemade module which also needs logging, so I copied/pasted the configuration of Log::Log4perl in it (I don't use an external configuration file for some reasons I'm not going to explain because it would be too long and off topic...) but the name of my .log file is a variable based on the date and time of my system, and I'm afraid that if there's a change in the time (like script launched at 12:12:12 and module called at 12:12:13), I'll end up with two .log files...
So after having though of some horrible methods on my own, this solution to call this variable from the main sript into the module seems perfect... Could someone please help me a little by giving me a very simple explanation on how I need to organize my script/module to make this work ?
I don't know what to give you as additional informations to help me, but here is my configuration of Log::Log4perl, maybe you'll see my problem more clearly ^^" :
my %month = ( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12', ); my $localdate = localtime; my ($dname, $mname, $day, $time, $year) = split( " ",$localdate); my $log_name = "./log/ST03_$year-$month{$mname}-$day\_$time.log"; my $conf = qq{ log4perl.rootLogger = DEBUG, myFILE log4perl.appender.myFILE = Log::Log4perl::App +ender::File log4perl.appender.myFILE.filename = $log_name log4perl.appender.myFILE.mode = append log4perl.appender.myFILE.layout = Log::Log4perl::Lay +out::PatternLayout log4perl.appender.myFILE.layout.ConversionPattern = %d + [%p] (%F line %L) %M %m%n }; Log::Log4perl::init( \$conf ); my $logger = Log::Log4perl->get_logger;
And here if what I'd hope to use in the module :
my $conf = qq{ log4perl.rootLogger = DEBUG, myFILE log4perl.appender.myFILE = Log::Log4perl::App +ender::File log4perl.appender.myFILE.filename = $main::log_name log4perl.appender.myFILE.mode = append log4perl.appender.myFILE.layout = Log::Log4perl::Lay +out::PatternLayout log4perl.appender.myFILE.layout.ConversionPattern = %d + [%p] (%F line %L) %M %m%n }; Log::Log4perl::init( \$conf ); # Charge la configuration my $logger = Log::Log4perl->get_logger;
Thank you, Regards
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |