|laziness, impatience, and hubris|
Re: Nervously....How do I access the Name of a Perl variable?by wog (Curate)
|on Jun 11, 2002 at 01:35 UTC||Need Help??|
Getting the line number and filename is relatively easy:
You can also get the subroutine its in. See the docs for caller.
Getting the names of the relevant variables is much harder, because perl does not such a convinent mechanism for compile-time expanded macros as C. However there are things called source filters, and the Filter::Simple module provides a nice interface to them. So, we can use that to make a module that adds a TRACE() macro:
(The module just needs to be saved appropriately to a file called by the name of Package.pm where Package is whatever's specified by the package declaration. (You could change that.))
This has the side-effect of allowing arbitrary expressions instead of just variable names.
update: Usage would be like:
If you want to change TRACE to trace (I prefer that compile-time-special things be uppercased), then it shouldn't be hard to fiquire that out.