Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to "disguise" caller

by menth0l (Monk)
on Feb 10, 2011 at 14:01 UTC ( #887451=perlquestion: print w/ replies, xml ) Need Help??
menth0l has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to change name of the caller module in runtime. For example:
sub get_caller() { print caller() . "\n"; } sub dummy() { my $dummy_module = " package DUMMY; sub wrapper {main::get_caller()}; 1; "; eval $dummy_module; my $dummy = {}; bless $dummy, 'DUMMY'; $dummy->wrapper(); } get_caller(); dummy();
Output:
main DUMMY
Is it possible to 'disguise' caller module in easier, more elegant way? I need it to log messages in log4perl as if they were logged from totally different module...

Comment on How to "disguise" caller
Select or Download Code
Re: How to "disguise" caller
by moritz (Cardinal) on Feb 10, 2011 at 14:17 UTC
    I need it to log messages in log4perl as if they were logged from totally different module...

    I don't know your larger background, and I can't answer your question, but want to point out that when parts of your software is lying to other parts of the software, that's a strong indication that there's a larger problem in the design of your code.

    Maybe logging is abused for something it was never meant to? Or maybe it's really an XY Problem?

    Also, would you care to provide more background information about why this is necessary? I'm really curious.

      I'd bet lunch he's mocking up output to feed a test without executing something undesirable, or something similar.

      In which case it would be... less bad.

      http://www.mpwilson.com/#pm
      Well, I got several objects of different classes which produce some data which is stored in a container object. When all workers finish, container renders reports and logs them. E.g.
      $container->do_FooBarWork(); # Foo::Bar work $container->do_BazWhoWork(); # Baz::Who work $container->createReport(); # renders report for each worker $container->dumpReport(); # dumps all reports to log
      Now all reports are logged by Container::dumpReport() method and thus log4perl can't tell which class produced which output. But if container class knew who produced what then it could log it "on behalf" of worker object. In other words i want to "tie" logged result to class that created it.

        This might be an easier solution:

        Have a method in each of the worker classes that does the logging regarding its activity, and then call these methods from the container class. That way the reporting also becomes customizable by overriting the reporting method in an inheriting class.

        The container can still do some summary logging, if necessary.

Re: How to "disguise" caller
by belden (Friar) on Feb 10, 2011 at 15:50 UTC
    It looks like you're trying to substitute behavior of something, perhaps for testing. Can you post a more representative snip of "here's the code that's written", and "here's what I'm doing now to solve my problem"? Currently they're both kind of tied up together. :)
Re: How to "disguise" caller
by DrHyde (Prior) on Feb 11, 2011 at 11:02 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://887451]
Approved by moritz
Front-paged by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-10-01 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (41 votes), past polls