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


In reply to Can I from within a module access variables from the calling program? by HJO

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.