Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: caller() mystery

by ikegami (Pope)
on Sep 26, 2008 at 09:47 UTC ( #713838=note: print w/replies, xml ) Need Help??


in reply to caller() mystery

There's only two levels of caller in this case. Do you expect it to return the name of your shell?
use strict; use warnings; sub _format_caller { my @x=caller($_[0]); print(join(',',@x[0..2]),"\n"); } sub f { my $level=$_[0]; print "test caller $level\n"; _format_caller($level); print("\n"); } f($_) for 0..2;
test caller 0 main,713838.pl,12 test caller 1 main,713838.pl,16 test caller 2 Use of uninitialized value in join or string at 713838.pl line 6. Use of uninitialized value in join or string at 713838.pl line 6. Use of uninitialized value in join or string at 713838.pl line 6. ,,

Replies are listed 'Best First'.
Re^2: caller() mystery
by rovf (Priest) on Sep 26, 2008 at 10:11 UTC
    There's only two levels of caller in this case.

    Hmmm.... I was counting 3:

    • Level 0: _format_caller
    • Level 1: main::f
    • Level 2: The main program itself

    It seems that the top-level program is not represented by a stack frame...
    -- 
    Ronald Fischer <ynnor@mm.st>

      caller(0) returns info about _format_caller's caller.
      caller(1) returns info about f's caller
      caller(2) would return info about the main program's caller, but that's no longer in Perl. What are you expecting it to return?

        Update Please disregard this posting. I finally understood the issue (see id:://713852).
        caller(1) returns info about f's caller

        If this is the case, I would understand why my approach failed. But then I don't understand why in my original program, caller(1) (executed from within _format_caller) delivered

        main,U:\develsv\ARTS\playground\callertest.pl,16,main::f,1,,,,2,UUUUUU +UUUUU
        That is, it says "the caller is main::f". This is _format_caller's caller, NOT f's caller. Actually that's why I had concluded that counting would start at 1. And indeed, from the command line:

        perl -lwe 'sub a{print((caller 0)[3])};sub b{a()};sub c{b()}; c'
        gives main::a (that is, caller 0 says "who I am", i.e. which function is invoking caller).
        perl -lwe 'sub a{print((caller 1)[3])};sub b{a()};sub c{b()}; c'
        gives main::b (that is, caller 1 says who has called me)
        perl -lwe 'sub a{print((caller 2)[3])};sub b{a()};sub c{b()}; c'
        gives main::c (that is, the caller of main::b), but for
        perl -lwe 'sub a{print((caller 3)[3])};sub b{a()};sub c{b()}; c'
        I would have expected to get information about the caller of main::c, which is the main program (not the shell), but there is none.

        -- 
        Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2020-10-20 23:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (212 votes). Check out past polls.

    Notices?