in reply to getting a subroutine's name

It doesn't return anything, because nothing has been called. Only defined.

Try

calling();

That said...

#works fine sub addem{ ($value1, $value2)=@_; $value1+$value2; print join(':',caller); ($package, $filename, $line)=caller; }

...will return 3 for any values you add. You are adding the two values passed and throw away the result. Then you return a list of three items (the last value evaluated in that subrouttine) - $package, $filename, $line - which in scalar context results in the number of elements in that list: 3.

Works fine? Not.

Replies are listed 'Best First'.
Re^2: getting a subroutine's name
by biohisham (Priest) on Jul 17, 2009 at 18:59 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. 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
      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