Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: Can I from within a module access variables from the calling program?

by Anonymous Monk
on Oct 29, 2012 at 10:47 UTC ( #1001342=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Can I from within a module access variables from the calling program?
in thread Can I from within a module access variables from the calling program?

And the use parent, what is it doing precisely please ?

If you're really interested, read about it :) perldoc parent

output

$ dir /b shabba.pl $ perl shabba.pl $ dir /b A1.2012-10-29T10-49-10Z.log A2.2012-10-29T10-49-11Z.log Logfile.2012-10-29T10-49-10Z.log shabba.pl $ cat A1.2012-10-29T10-49-10Z.log 2012/10/29 03:49:10 INFO> shabba.pl:43 main::Main - (Groceries) Import +ant Info! 2012/10/29 03:49:10 INFO> shabba.pl:44 main::Main - (Groceries) Import +ant Info! THEN NEWLINE THEN IMPORTANTINFO $ cat A2.2012-10-29T10-49-11Z.log 2012/10/29 03:49:10 INFO> shabba.pl:48 main::Main - (Junk) UnImportant + Info! $ cat Logfile.2012-10-29T10-49-10Z.log 2012/10/29 03:49:10 shabba.pl 43> (Groceries) Important Info! 2012/10/29 03:49:10 shabba.pl 44> (Groceries) Important Info! THEN NEW +LINE THEN IMPORTANTINFO 2012/10/29 03:49:10 shabba.pl 48> (Junk) UnImportant Info! 2012/10/29 03:49:10 shabba.pl 83> (Bar.Twix) Twix mjam 2012/10/29 03:49:10 shabba.pl 83> (Bar.Twix) Twix mjam 2012/10/29 03:49:10 shabba.pl 83> (Bar.Twix) YAM NEWLINE Twix mjam 2012/10/29 03:49:10 shabba.pl 83> (Bar.Twix) YAM NEWLINE Twix mjam 2012/10/29 03:49:10 shabba.pl 87> (Bar.Mars) Mars mjam 2012/10/29 03:49:10 shabba.pl 87> (Bar.Mars) Mars mjam 2012/10/29 03:49:10 shabba.pl 87> (Bar.Mars) YAM NEWLINE Mars mjam 2012/10/29 03:49:10 shabba.pl 87> (Bar.Mars) YAM NEWLINE Mars mjam

testcase (creates 3 logfiles on each run)

#!/usr/bin/perl -- use Path::Class; use constant THISFILE => file( __FILE__ )->absolute->stringify; use constant THISDIR => file( THISFILE )->dir->stringify; use strict; use warnings; use Log::Log4perl; chdir THISDIR or die Fudge( 'chdir', THISDIR ); Main( @ARGV ); exit( 0 ); sub Main { my $logconfig = q{; log4perl.logger.Groceries=DEBUG, A1 log4perl.appender.A1=Log::Log4perl::Appender::HJOfilnamed log4perl.appender.A1.mode=append log4perl.appender.A1.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.A1.layout.ConversionPattern=%d %p> %F{1}:%L %M - (%c +) %m%n log4perl.logger.Junk=DEBUG, A2 log4perl.appender.A2=Log::Log4perl::Appender::HJOfilnamed log4perl.appender.A2.mode=append log4perl.appender.A2.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.A2.layout.ConversionPattern=%d %p> %F{1}:%L %M - (%c +) %m%n log4perl.debug=true log4perl.b=DEBUG, A2 log4perl.category.Bar.Twix = DEBUG, Logfile log4perl.category.Bar.Mars = DEBUG, Logfile log4perl.appender.Logfile = Log::Log4perl::Appender::HJOfilna +med log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLay +out log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> (%c) + %m %n log4perl.rootLogger = DEBUG, Logfile }; Log::Log4perl::init( \$logconfig ); { my $log = Log::Log4perl::get_logger("Groceries"); $log->info("Important Info!"); $log->info("Important Info! THEN NEWLINE\nTHEN IMPORTANTINFO") +; } { my $log = Log::Log4perl::get_logger("Junk"); $log->info("UnImportant Info!"); } Bar::Twix::eat(); Bar::Mars::eat(); } BEGIN{ $INC{'Log/Log4perl/Appender/HJOfilnamed.pm'} = __FILE__; package Log::Log4perl::Appender::HJOfilnamed; use parent qw[ Log::Log4perl::Appender::File ]; use POSIX(); sub new { my $class = shift; my %opts = @_; my $tim = POSIX::strftime('%Y-%m-%dT%H-%M-%SZ', gmtime); my $fname = join '.', grep defined, $opts{name}, $opts{appende +r}, $tim, 'log'; $fname =~ s{::}{.}g; $fname =~ s{[^0-9a-zA-Z\-\+\.]}{}g; $fname = $1 if $fname =~ /^(.*)$/; return $class->SUPER::new( @_, filename => $fname ); } } sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; } BEGIN { package Bar::Twix; use Log::Log4perl qw(:easy); sub eat { DEBUG("Twix mjam");INFO("YAM NEWLINE\nTwix mjam"); } package Bar::Mars; use Log::Log4perl qw(:easy); sub eat { INFO("Mars mjam");INFO("YAM NEWLINE\nMars mjam"); } }


Comment on Re^4: Can I from within a module access variables from the calling program?
Select or Download Code
Re^5: Can I from within a module access variables from the calling program?
by HJO (Acolyte) on Oct 29, 2012 at 11:48 UTC

    Thanks for the perldoc link, I've read it and will maybe hopefully understand its implications someday...

    About your code, I think I got it (not quite sure ^^'), but it's not really an anwser to my issue... I'm looking at a way to have the same logfile name for my script and module while I'm running them... And I'm afraid that as you had, if there's a 1 second delay, the two files would not have the same name...

      if there's a 1 second delay, the two files would not have the same name...

      give gmtime $^T

        FWIW, you're only supposed to configure the logger in one place once, the app

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001342]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2015-07-03 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls