Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6384439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:46:33+00:00 2026-05-25T02:46:33+00:00

In Perl, is there any way to tell which .pl script has initialized this

  • 0

In Perl, is there any way to tell which .pl script has initialized this instance of a module?

Specifically, I’d like to get the name of the script calling a module, which has a Log4perl object it. That way, I’ll know which .log file I want to write to within the module.

Am I doing this wrong? If I define the $logger in my .pl script, will any $logger calls within the module write to the same .log file as the calling script?

I don’t have any sample code yet, but have been reading up on Log4perl. Basically, if I set an Appender to a file, caller.log, which is the file appender for my calling script, caller.pl, I’d want any logging defined in the custom imported module, to also write to caller.log (implicitly, if possible — obviously I could just pass the name of the log name when I initialize the module instance).

Is this possible without passing arguments specifying which File Appender the module should write to? Doesn’t Log4perl use just one $logger instance?

Also, let me know if I’m way out, and if there’s a different approach I should be considering.

Thank you

EDIT: Sorry, after I posted this, I looked at the Related Links, and I guess my search wording just wasn’t correct. It looks like this is a pretty good solution: Self logging Perl modules (without Moose)

If anyone has any other ideas, though, please let me know.

EDIT 2: Finally tested, and got it to work as I had wanted — was a lot easier than was making it out to be, too!

This is my setup, pretty much:

Module.pm

package Module;

use Log::Log4perl qw(get_logger :levels);
use Data::Dumper;

my $logger = get_logger("Module");

sub new {
    my ($class, $name) = @_;

    my @caller = caller(0);
    $logger->debug("Creating new Module. Called by " . Dumper(\@caller));

    my $object = { 'name' => $name };

    return bless($object, $class);  
}

caller.pl

use Module;
use Log::Log4perl qw(get_logger :levels);
use Data::Dumper;

my $PATH = "$ENV{'APPS'}/$ENV{'OUTDIR'}";
my $SCRIPT = "caller";

my $logger = get_logger("Module");
$logger->level($DEBUG);

my $file_appender = Log::Log4perl::Appender->new("Log::Dispatch::File", 
                        filename=> "$PATH/$SCRIPT.log", 
                        mode => "append",);
$logger->add_appender($file_appender);

my $layout = Log::Log4perl::Layout::PatternLayout->new("%d %p> %F{1}:%L %M - %m%n");
$file_appender->layout($layout);

my $lib = Module->new('Chris');

$logger->info(Dumper($lib));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:46:34+00:00Added an answer on May 25, 2026 at 2:46 am

    You could subclass Log4perl, overriding its constructor. In your custom constructor, use caller() to get the filename that called the constructor and put it in $self.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any easy way to tell perl now ignore everything that is printed?
Is there any way to get Perl to convert the stringified version e.g (ARRAY(0x8152c28))
While using perl debugger, is there any way to step out of the current
How to manage user session in Perl CGI web application? Is there any way
Are there any easy installation tools like perl -MCPAN -e shell; to install python
In Perl there is a very handy module, Term::VT102, which allows you to create
Is there any way to use a script to add files to an Xcode
Is there any way to check whitch of perl command line switches have been
Is there any way with the standard perl libraries to open a file and
Is there any way to change the end of line character for a perl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.