======================================================================== * README.md ======================================================================== # NAME Devel::StackTrace - An object representing a stack trace # VERSION version 2.04 # SYNOPSIS use Devel::StackTrace; my $trace = Devel::StackTrace->new; print $trace->as_string; # like carp # from top (most recent) of stack to bottom. while ( my $frame = $trace->next_frame ) { print "Has args\n" if $frame->hasargs; } # from bottom (least recent) of stack to top. while ( my $frame = $trace->prev_frame ) { print "Sub: ", $frame->subroutine, "\n"; } # DESCRIPTION The `Devel::StackTrace` module contains two classes, `Devel::StackTrace` and [Devel::StackTrace::Frame](https://metacpan.org/pod/Devel::StackTrace::Frame). These objects encapsulate the information that can retrieved via Perl's `caller` function, as well as providing a simple interface to this data. The `Devel::StackTrace` object contains a set of `Devel::StackTrace::Frame` objects, one for each level of the stack. The frames contain all the data available from `caller`. This code was created to support my [Exception::Class::Base](https://metacpan.org/pod/Exception::Class::Base) class (part of [Exception::Class](https://metacpan.org/pod/Exception::Class)) but may be useful in other contexts. # 'TOP' AND 'BOTTOM' OF THE STACK When describing the methods of the trace object, I use the words 'top' and 'bottom'. In this context, the 'top' frame on the stack is the most recent frame and the 'bottom' is the least recent. Here's an example: foo(); # bottom frame is here sub foo { bar(); } sub bar { Devel::StackTrace->new; # top frame is here. } # METHODS This class provide the following methods: ## Devel::StackTrace->new(%named\_params) Returns a new Devel::StackTrace object. Takes the following parameters: - frame\_filter => $sub By default, Devel::StackTrace will include all stack frames before the call to its constructor. However, you may want to filter out some frames with more granularity than 'ignore\_package' or 'ignore\_class' allow. You can provide a subroutine which is called with the raw frame data for each frame. This is a hash reference with two keys, "caller", and "args", both of which are array references. The "caller" key is the raw data as returned by Perl's `caller` function, and the "args" key are the subroutine arguments found in `@DB::args`. The filter should return true if the frame should be included, or false if it should be skipped. - filter\_frames\_early => $boolean If this parameter is true, `frame_filter` will be called as soon as the stacktrace is created, and before refs are stringified (if `unsafe_ref_capture` is not set), rather than being filtered lazily when [Devel::StackTrace::Frame](https://metacpan.org/pod/Devel::StackTrace::Frame) objects are first needed. This is useful if you want to filter based on the frame's arguments and want to be able to examine object properties, for example. - ignore\_package => $package\_name OR \\@package\_names Any frames where the package is one of these packages will not be on the stack. - ignore\_class => $package\_name OR \\@package\_names Any frames where the package is a subclass of one of these packages (or is the same package) will not be on the stack. Devel::StackTrace internally adds itself to the 'ignore\_package' parameter, meaning that the Devel::StackTrace package is **ALWAYS** ignored. However, if you create a subclass of Devel::StackTrace it will not be ignored. - skip\_frames => $integer This will cause this number of stack frames to be excluded from top of the stack trace. This prevents the frames from being captured at all, and applies before the `frame_filter`, `ignore_package`, or `ignore_class` options, even with `filter_frames_early`. - unsafe\_ref\_capture => $boolean If this parameter is true, then Devel::StackTrace will store references internally when generating stacktrace frames. **This option is very dangerous, and should never be used with exception objects**. Using this option will keep any objects or references alive past their normal lifetime, until the stack trace object goes out of scope. It can keep objects alive even after their `DESTROY` sub is called, resulting it it being called multiple times on the same object. If not set, Devel::StackTrace replaces any references with their stringified representation. - no\_args => $boolean If this parameter is true, then Devel::StackTrace will not store caller arguments in stack trace frames at all. - respect\_overload => $boolean By default, Devel::StackTrace will call `overload::AddrRef` to get the underlying string representation of an object, instead of respecting the object's stringification overloading. If you would prefer to see the overloaded representation of objects in stack traces, then set this parameter to true. - max\_arg\_length => $integer By default, Devel::StackTrace will display the entire argument for each subroutine call. Setting this parameter causes truncates each subroutine argument's string representation if it is longer than this number of characters. - message => $string By default, Devel::StackTrace will use 'Trace begun' as the message for the first stack frame when you call `as_string`. You can supply an alternative message using this option. - indent => $boolean If this parameter is true, each stack frame after the first will start with a tab character, just like `Carp::confess`. ## $trace->next\_frame Returns the next [Devel::StackTrace::Frame](https://metacpan.org/pod/Devel::StackTrace::Frame) object on the stack, going down. If this method hasn't been called before it returns the first frame. It returns `undef` when it reaches the bottom of the stack and then resets its pointer so the next call to `$trace->next_frame` or `$trace->prev_frame` will work properly. ## $trace->prev\_frame Returns the next [Devel::StackTrace::Frame](https://metacpan.org/pod/Devel::StackTrace::Frame) object on the stack, going up. If this method hasn't been called before it returns the last frame. It returns undef when it reaches the top of the stack and then resets its pointer so the next call to `$trace->next_frame` or `$trace->prev_frame` will work properly. ## $trace->reset\_pointer Resets the pointer so that the next call to `$trace->next_frame` or `$trace->prev_frame` will start at the top or bottom of the stack, as appropriate. ## $trace->frames When this method is called with no arguments, it returns a list of [Devel::StackTrace::Frame](https://metacpan.org/pod/Devel::StackTrace::Frame) objects. They are returned in order from top (most recent) to bottom. This method can also be used to set the object's frames if you pass it a list of [Devel::StackTrace::Frame](https://metacpan.org/pod/Devel::StackTrace::Frame) objects. This is useful if you want to filter the list of frames in ways that are more complex than can be handled by the `$trace->filter_frames` method: $stacktrace->frames( my_filter( $stacktrace->frames ) ); ## $trace->frame($index) Given an index, this method returns the relevant frame, or undef if there is no frame at that index. The index is exactly like a Perl array. The first frame is 0 and negative indexes are allowed. ## $trace->frame\_count Returns the number of frames in the trace object. ## $trace->as\_string(\\%p) Calls `$frame->as_string` on each frame from top to bottom, producing output quite similar to the Carp module's cluck/confess methods. The optional `\%p` parameter only has one option. The `max_arg_length` parameter truncates each subroutine argument's string representation if it is longer than this number of characters. If all the frames in a trace are skipped then this just returns the `message` passed to the constructor or the string `"Trace begun"`. ## $trace->message Returns the message passed to the constructor. If this wasn't passed then this method returns `undef`. # SUPPORT Bugs may be submitted at [https://github.com/houseabsolute/Devel-StackTrace/issues](https://github.com/houseabsolute/Devel-StackTrace/issues). I am also usually active on IRC as 'autarch' on `irc://irc.perl.org`. # SOURCE The source code repository for Devel-StackTrace can be found at [https://github.com/houseabsolute/Devel-StackTrace](https://github.com/houseabsolute/Devel-StackTrace). # DONATIONS If you'd like to thank me for the work I've done on this module, please consider making a "donation" to me via PayPal. I spend a lot of free time creating free software, and would appreciate any support you'd care to offer. Please note that **I am not suggesting that you must do this** in order for me to continue working on this particular software. I will continue to do so, inasmuch as I have in the past, for as long as it interests me. Similarly, a donation made in this way will probably not make me work on this software much more, unless I get so many donations that I can consider working on free software full time (let's all have a chuckle at that together). To donate, log into PayPal and send money to autarch@urth.org, or use the button at [http://www.urth.org/~autarch/fs-donation.html](http://www.urth.org/~autarch/fs-donation.html). # AUTHOR Dave Rolsky # CONTRIBUTORS - Dagfinn Ilmari Mannsåker - David Cantrell - Graham Knop - Ivan Bessarabov - Mark Fowler - Pali - Ricardo Signes # COPYRIGHT AND LICENSE This software is Copyright (c) 2000 - 2019 by David Rolsky. This is free software, licensed under: The Artistic License 2.0 (GPL Compatible) The full text of the license can be found in the `LICENSE` file included with this distribution. ======================================================================== * LICENSE ======================================================================== This software is Copyright (c) 2000 - 2019 by David Rolsky. This is free software, licensed under: The Artistic License 2.0 (GPL Compatible) The Artistic License 2.0 Copyright (c) 2000-2006, The Perl Foundation. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble This license establishes the terms under which a given free software Package may be copied, modified, distributed, and/or redistributed. The intent is that the Copyright Holder maintains some artistic control over the development of that Package while still keeping the Package available as open source and free software. You are always permitted to make arrangements wholly outside of this license directly with the Copyright Holder of a given Package. If the terms of this license do not permit the full use that you propose to make of the Package, you should contact the Copyright Holder and seek a different licensing arrangement. Definitions "Copyright Holder" means the individual(s) or organization(s) named in the copyright notice for the entire Package. "Contributor" means any party that has contributed code or other material to the Package, in accordance with the Copyright Holder's procedures. "You" and "your" means any person who would like to copy, distribute, or modify the Package. "Package" means the collection of files distributed by the Copyright Holder, and derivatives of that collection and/or of those files. A given Package may consist of either the Standard Version, or a Modified Version. "Distribute" means providing a copy of the Package or making it accessible to anyone else, or in the case of a company or organization, to others outside of your company or organization. "Distributor Fee" means any fee that you charge for Distributing this Package or providing support for this Package to another party. It does not mean licensing fees. "Standard Version" refers to the Package if it has not been modified, or has been modified only in ways explicitly requested by the Copyright Holder. "Modified Version" means the Package, if it has been changed, and such changes were not explicitly requested by the Copyright Holder. "Original License" means this Artistic License as Distributed with the Standard Version of the Package, in its current version or as it may be modified by The Perl Foundation in the future. "Source" form means the source code, documentation source, and configuration files for the Package. "Compiled" form means the compiled bytecode, object code, binary, or any other form resulting from mechanical transformation or translation of the Source form. Permission for Use and Modification Without Distribution (1) You are permitted to use the Standard Version and create and use Modified Versions for any purpose without restriction, provided that you do not Distribute the Modified Version. Permissions for Redistribution of the Standard Version (2) You may Distribute verbatim copies of the Source form of the Standard Version of this Package in any medium without restriction, either gratis or for a Distributor Fee, provided that you duplicate all of the original copyright notices and associated disclaimers. At your discretion, such verbatim copies may or may not include a Compiled form of the Package. (3) You may apply any bug fixes, portability changes, and other modifications made available from the Copyright Holder. The resulting Package will still be considered the Standard Version, and as such will be subject to the Original License. Distribution of Modified Versions of the Package as Source (4) You may Distribute your Modified Version as Source (either gratis or for a Distributor Fee, and with or without a Compiled form of the Modified Version) provided that you clearly document how it differs from the Standard Version, including, but not limited to, documenting any non-standard features, executables, or modules, and provided that you do at least ONE of the following: (a) make the Modified Version available to the Copyright Holder of the Standard Version, under the Original License, so that the Copyright Holder may include your modifications in the Standard Version. (b) ensure that installation of your Modified Version does not prevent the user installing or running the Standard Version. In addition, the Modified Version must bear a name that is different from the name of the Standard Version. (c) allow anyone who receives a copy of the Modified Version to make the Source form of the Modified Version available to others under (i) the Original License or (ii) a license that permits the licensee to freely copy, modify and redistribute the Modified Version using the same licensing terms that apply to the copy that the licensee received, and requires that the Source form of the Modified Version, and of any works derived from it, be made freely available in that license fees are prohibited but Distributor Fees are allowed. Distribution of Compiled Forms of the Standard Version or Modified Versions without the Source (5) You may Distribute Compiled forms of the Standard Version without the Source, provided that you include complete instructions on how to get the Source of the Standard Version. Such instructions must be valid at the time of your distribution. If these instructions, at any time while you are carrying out such distribution, become invalid, you must provide new instructions on demand or cease further distribution. If you provide valid instructions or cease distribution within thirty days after you become aware that the instructions are invalid, then you do not forfeit any of your rights under this license. (6) You may Distribute a Modified Version in Compiled form without the Source, provided that you comply with Section 4 with respect to the Source of the Modified Version. Aggregating or Linking the Package (7) You may aggregate the Package (either the Standard Version or Modified Version) with other packages and Distribute the resulting aggregation provided that you do not charge a licensing fee for the Package. Distributor Fees are permitted, and licensing fees for other components in the aggregation are permitted. The terms of this license apply to the use and Distribution of the Standard or Modified Versions as included in the aggregation. (8) You are permitted to link Modified and Standard Versions with other works, to embed the Package in a larger work of your own, or to build stand-alone binary or bytecode versions of applications that include the Package, and Distribute the result without restriction, provided the result does not expose a direct interface to the Package. Items That are Not Considered Part of a Modified Version (9) Works (including, but not limited to, modules and scripts) that merely extend or make use of the Package, do not, by themselves, cause the Package to be a Modified Version. In addition, such works are not considered parts of the Package itself, and are not subject to the terms of this license. General Provisions (10) Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license. (11) If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license. (12) This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder. (13) This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed. (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.