Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!olivea!orc!inews!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: knowing your current context Message-ID: <1990Oct13.064058.6713@iwarp.intel.com> Date: 13 Oct 90 06:40:58 GMT References: <107126@convex.convex.com> <107138@convex.convex.com> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 50 In-Reply-To: tchrist@convex.COM (Tom Christiansen) In article <107138@convex.convex.com>, tchrist@convex (Tom Christiansen) writes: | In article <107126@convex.convex.com> I, myself, wrote: | >Anybody know how to pull out the current file, line, package, and | >subroutine from within a perl script? | I keep forgetting the new functions from pl>18 -- __LINE__ and __FILE__ | should do for those, although I'm still looking for package and subroutine. Done in Perl 4.0 (no kidding), as in "the version described by The Book". It's called "caller". Here's the doc (vaguely -ms like with the .P/.Ps/.Pe macros for "printout" font): | .NH 2 | .P caller | .Ps | caller(EXPR) | caller EXPR | caller | .Pe | .PP | Returns information about the subroutine call stack. Without an argument | it returns the package name, file name and line number that currently | executing subroutine was called from: | .Ps | ($package, $filename, $line) = caller; | .Pe | With an argument it evaluates EXPR as the number of stack frames to go back | before the current one. It also reports some additional information, and | magically sets | .P @DB\'args | to the arguments passed in that stack frame. It's primarily used by the | debugger to print out stack traces. | .Ps | $i = 0; | while (($pack, $file, $line, $subname, $hasargs, $wantarray) | = caller($i++)) { | .\|.\|. | } | .Pe Coming "Real Soon Now" to a source distribution near you. (Dare I say that the book has already gone to the publisher for production? Naaaah, I dare not. :-) print 'Just another ', ('a'..'z','A'..'Z')[41,4,17,11], ' hacker,' -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/