Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Where was I called?

by Ovid (Cardinal)
on Sep 28, 2006 at 10:13 UTC ( #575320=perlquestion: print w/replies, xml ) Need Help??

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

The problem is a long story, but the minimal test case is pretty clear. I have a subroutine which needs to know where is was called. For example:

while ( my $value = some_sub() ) { # do something my $other_value = some_sub(); }

In the above case, it's pretty easy to disambiguate:

sub some_sub { my @caller = caller(); # now I know where I was called }

That assigns the package, filename, and line number to @caller. However, there's a very weird edge case here:

for ( 1 .. 10 ) { my ( $foo, $bar ) = ( some_sub(), some_sub() ); # do stuff }

@caller will hold the same value for each of those. I'm overriding a pre-existing interface, so I'm not at liberty to change said interface. Without more information available, is there any way in &some_sub to know which invocation on a line was called? I can't hardcode any knowledge about the structure of calling lines because this may be called in many different places.


New address of my CGI Course.

Replies are listed 'Best First'.
Re: Where was I called?
by BrowserUk (Pope) on Sep 28, 2006 at 12:54 UTC

    Presumably this is for some kind of error reporting? How about simply reformatting the source code as:

    for ( 1 .. 10 ) { my ( $foo, $bar ) = ( some_sub(), some_sub() ); # do stuff }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      That's one thought, but it turns out there's a bug in the scheme.

      while ( my $value = some_sub() ) { # line 50 my $next_value = some_sub(); # line 51 }

      This is something I forgot about, but it's very, very irritating. The first time some_sub() is called, caller will correctly report that line 50 is the calling line and the second time some_sub() is called, it reports line 51. However, the second time through the while loop, the first some_sub() is reported as being on line 51! That totally blows my initial scheme.


      New address of my CGI Course.

        Wow, thanks Ovid! I never realized that problem existed. Just to explain, whenever a nextstate op is seen the global variable PL_curcop is set and that's some of what caller() uses. You know line 50 as you enter the loop, then the first line in the loop is 51 but you never reach that outer nextstate op to say you're testing a condition on line 50 again. I bet if you were really interested in solving this problem in a JFDI way that you could change that "goto 9" to be "goto 2" or promote the conditional expression into a do{} block. But that way lies madness so you won't do that.

        ### caller() will report line 50. 2 <;> nextstate(main 2 x:50) v 3 <{> enterloop(next->8 last->d redo->4) v 9 <0> pushmark s a <#> gv[*X] s/EARLYCV b <1> entersub[t2] sKS/TARG,1 c <|> and(other->4) vK/1 ### caller() will report line 51 4 <;> nextstate(main 1 x:51) v 5 <0> pushmark s 6 <#> gv[*Y] s/EARLYCV 7 <1> entersub[t4] vKS/TARG,1 8 <0> unstack v goto 9 d <2> leaveloop vK/2 e <@> leave[1 ref] vKP/REFC

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Where was I called?
by cephas (Pilgrim) on Sep 28, 2006 at 13:20 UTC
    This is less than ideal, but it gives you a little better visibility than just line number.
    { my ($line,$occurrence) = (0,0); sub some_sub { $, = ' - '; my @caller = caller(); if($caller[2] == $line) { $occurrence++; } else { $line = $caller[2]; $occurrence = 0; } print @caller, $occurrence, $/; } }
    You example shows the biggest limitation to this approach. $occurrence will just keep growing since it is the same line number. But if you know you are dealing with a specific number of calls on the line, you can mod whatever and get the specific occurence on the line.
Re: Where was I called?
by Anonymous Monk on Sep 28, 2006 at 11:08 UTC
    The simplest way is to give this problem to Simon Cozens and claim it can't be done.
      Which is the same way one of the head developers here at $work endorsed as "the best way to obtain things from system administrators". Never ask "Could you...?", but "Is it possible to...?"
Re: Where was I called?
by monkey_boy (Priest) on Sep 28, 2006 at 10:42 UTC
    Would the use of caller combined with a Time-stamp help?
    The only way i can differentiate between the two is that one is executed before the other.

    This is not a Signature...
Re: Where was I called?
by Anonymous Monk on Sep 28, 2006 at 10:41 UTC
    use B;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://575320]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2021-05-15 14:59 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (150 votes). Check out past polls.