Getting the name of a function (sub) inside it 
Author Message
 Getting the name of a function (sub) inside it

Hi perl gurus,

  Just wanted to know if there is a way I can access the name of a
function inside it.

For debugging purposes I do this:

sub main
{
   if ($DEBUG) {print "In main()" }
   .
   .
   .

Quote:
}

It's just that if I change the name of the function I'll have to change
the debug line too.

If would be easier if I can know the name of the function, just as you
can do __FILE__ , __LINE__ etc.

thanks!

Sudhir



Thu, 20 May 2004 08:54:32 GMT  
 Getting the name of a function (sub) inside it

Quote:

> Hi perl gurus,

>   Just wanted to know if there is a way I can access the name of a
> function inside it.

> For debugging purposes I do this:

> sub main
> {
>    if ($DEBUG) {print "In main()" }
>    .
>    .
>    .
> }

> It's just that if I change the name of the function I'll have to change
> the debug line too.

> If would be easier if I can know the name of the function, just as you
> can do __FILE__ , __LINE__ etc.

perldoc -f caller

--
Klein bottle for rent - inquire within.



Thu, 20 May 2004 09:19:02 GMT  
 Getting the name of a function (sub) inside it

Quote:
>   Just wanted to know if there is a way I can access the name of a
> function inside it.

  sub foo
  {
    print 'In function: ', (caller(0))[3], "\n";
  }

  foo;

'perldoc -f caller' for details.

--
Craig Berry <http://www.cinenet.net/~cberry/>
"That which is now known, was once only imagined." - William Blake



Thu, 20 May 2004 20:18:35 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. getting the name of a sub from a sub ref

2. sub name (&$) vs sub name ($&)

3. getting the name of a sub

4. Getting the subroutine name from within a sub

5. Outer scope of a sub inside a sub

6. Error : calling sub routine inside another sub routine

7. anonymous sub inside anonymous sub?

8. Q: calling C functions with same name as sub (IO::File::open)

9. Getting the name of the calling function

10. get name of previous sub that called the actual sub

11. Differences between anonymous subs and named subs

12. Finding sub name from sub pointer

 

 
Powered by phpBB® Forum Software