APM: How to find out my current subroutine name?

Mike South msouth at fulcrum.org
Sat Apr 12 09:09:46 CDT 2003


>From austin-admin at mail.pm.org  Sat Apr 12 08:44:31 2003
>X-Sender: horshack at gatekeeper.rosi13.de
>
>Hi, for debugging purposes I want to find out the name of the current 
>subroutine. How can I find it out?
>
>I usually do this, which is poor:
>
>sub drink {
>         my $subr = "drink";
>         printf "%s is not yet implemented\n", $subr;
>}
>
>I tried caller(0), caller(1),... but it never tell me the subroutines name.
>Thanks for hints,
>Horshack

caller() is weird, and perldoc -f caller doesn't really help much.  You
need to call it in list context with an argument of 0 and then item 3 of
the array it returns will be what you want.

	&foo;

	sub foo {
		print "the sub is ", (caller(0))[3], "\n";
		# or, less fancily
		@yo = caller(0);
		print "(again) the sub is ", $yo[3],"\n";
	}

__END__

Unrelated aside:

For a second I thought I was going crazy because I had

		print (caller(0))[3];

and it was giving me a syntax error at ')['.  Then I realized (I guess)
that it was because that opening paren belongs to print().  I
couldn't think of any satisfying way to get around that.  Prepending
with ''. was the best I could come up wtih.  (You can name STDOUT
explicitly, but that's both more typing and not equivalent, since
STDOUT might not have been selected).

mike

PS  I joined this list because my job was going to move me to 
Austin.  That fell through, but we are still looking at 
moving there (to be near family).  If anyone knows of any
perl jobs there I would appreciate an email.  I have a
resume and perl summary up at http://fulcrum.org/msouth/resume.txt
and http://fulcrum.org/msouth/perl_summary.txt.




More information about the Austin mailing list