in reply to Re: getting a subroutine's name
in thread getting a subroutine's name

this code would work fine even if that caller assignment in list context is commmented out since caller works in list and scalar context as well. in the second code however, the problem arises since I am putting a subroutine call inside a subroutine of its own, and the same exact code is here in the book and the return value is supposed be so I am wondering if there is something different
the-calling-subroutine-name main, directory/program.pl,line,main::the-calling-subroutine-name
#works fine sub addem{ ($value1, $value2)=@_; $value1+$value2; print join(':',caller); # ($package, $filename, $line)=caller; }
even this code works fine if I am not calling from within a subroutine the addem() function,
#notice disablement of the blocks in the callingfun() makes it work sub addem{ ($value1,$value2)=@_; $value1+$value2; print join(',',caller); } #sub callingfun{ $result = addem(2,2); # }
so I don't really understand the behavior, tried getting a diagnosis using eval function but did not work out..........
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

Replies are listed 'Best First'.
Re^3: getting a subroutine's name
by shmem (Chancellor) on Jul 17, 2009 at 19:20 UTC
    this code would work fine even if that caller assignment in list context is commmented out since caller works in list and scalar context as well.

    Yes, it "works", but it does not return what is expected - the sum of the values passed in.

    in the second code however, the problem arises since I am putting a subroutine call inside a subroutine of its own, and the same exact code is here in the book and the return value is supposed be so I am wondering if there is something different

    caller works fine in subroutine chains, but the subroutines must be called, which you don't do in your "second code."

    Try the following:

    sub calling { $value = addem(2,2); } sub addem { ($value1, $value2) = @_; print join(",", caller 0); $value1+$value2; } # this is the missing piece to make the above output something. # call the subroutine! calling();
      OoPs.. :p, that jumped over my head when you said try calling() I thought calling was like another function similar to caller....never mind this stupidity...hehe..thanks a lot anyways...you saved me from myself
      Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind