nneul has asked for the wisdom of the Perl Monks concerning the following question:

What am I doing wrong here? I get partially correct information from caller(), but sometimes the filenames seem to be off by one. Background: sub UsageLogger() does a call trace, and sends it to a central server via UDP for tracking local API and script usage. Code in UsageLogger routine looks like:
(undef, $function_basefile, $function_line, $function) = caller(1); (undef, $caller_basefile, $caller_line, $caller) = caller(2);
Sometimes I get the correct info, where the function matches up with function_basefile. But other times, it doesn't seem to match. The function is correct, but the filename is from the caller's caller. The reason for 1,2 is that the call trace looks like:
main -> sub1 -> sub2 -> UsageLogger
For that example, I want to see sub2 as the function, and sub1 as the caller, with the appropriate filenames. Here's an example:
Mon Mar 7 07:09:12 2005 syscron.cc.umr.edu/syscron.cc.umr.edu unixdb: + script = /afs/umr.edu/software/unixdb/bin/load-email-usage-userid.pl + function = UMR::SysProg::ADSObject::new function_file = /afs/umr +.edu/software/umrperl/libs/UMR/SysProg/Exchange.pm function_line += 43 caller = UMR::SysProg::Exchange::new caller_file = /afs/ +umr.edu/software/unixdb/bin/load-email-usage-userid.pl caller_l +ine = 8 cwd = /afs/umr.edu/software/unixdb
ADSObject::new should have been from ADSObject.pm, not Exchange.pm. Exchange::new should have been from Exchange.pm. I saw the note in caller's documentation about losing call frames due to optimization for N > 1, but that doesn't seem to apply here, and I would expect to lose the subroutine call as well, not just the filename.

Replies are listed 'Best First'.
Re: Partially wrong info from caller()
by tilly (Archbishop) on Mar 14, 2005 at 17:18 UTC
    I am unable to reproduce your result, and so cannot say what you're doing right or wrong.

    Can you produce a minimal example that illustrates the bug?

    (My personal guess would be that someone did some cut-and-paste, so the function that is supposed to be in one module also exists in the other, and Perl is telling you the truth. Excessive use of cut-and-paste is a different problem.)