This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
aa714db47a08b7b98af776f1ce4d4be76dd95228
[perl5.git] / cpan / Pod-Simple / lib / Pod / Simple / Debug.pm
1 require 5;
2 package Pod::Simple::Debug;
3 use strict;
4 use vars qw($VERSION );
5 $VERSION = '3.36';
6
7 sub import {
8   my($value,$variable);
9   
10   if(@_ == 2) {
11     $value = $_[1];
12   } elsif(@_ == 3) {
13     ($variable, $value) = @_[1,2];
14     
15     ($variable, $value) = ($value, $variable)
16        if     defined $value    and ref($value)    eq 'SCALAR'
17       and not(defined $variable and ref($variable) eq 'SCALAR')
18     ; # tolerate getting it backwards
19     
20     unless( defined $variable and ref($variable) eq 'SCALAR') {
21       require Carp;
22       Carp::croak("Usage:\n use Pod::Simple::Debug (NUMVAL)\nor"
23                 . "\n use Pod::Simple::Debug (\\\$var, STARTNUMVAL)\nAborting");
24     }
25   } else {
26     require Carp;
27     Carp::croak("Usage:\n use Pod::Simple::Debug (NUMVAL)\nor"
28                     . "\n use Pod::Simple::Debug (\\\$var, STARTNUMVAL)\nAborting");
29   }
30
31   if( defined &Pod::Simple::DEBUG ) {
32     require Carp;
33     Carp::croak("It's too late to call Pod::Simple::Debug -- "
34               . "Pod::Simple has already loaded\nAborting");
35   }
36   
37   $value = 0 unless defined $value;
38
39   unless($value =~ m/^-?\d+$/) {
40     require Carp;
41     Carp::croak( "$value isn't a numeric value."
42             . "\nUsage:\n use Pod::Simple::Debug (NUMVAL)\nor"
43                     . "\n use Pod::Simple::Debug (\\\$var, STARTNUMVAL)\nAborting");
44   }
45
46   if( defined $variable ) {
47     # make a not-really-constant
48     *Pod::Simple::DEBUG = sub () { $$variable } ;
49     $$variable = $value;
50     print STDERR "# Starting Pod::Simple::DEBUG = non-constant $variable with val $value\n";
51   } else {
52     *Pod::Simple::DEBUG = eval " sub () { $value } ";
53     print STDERR "# Starting Pod::Simple::DEBUG = $value\n";
54   }
55   
56   require Pod::Simple;
57   return;
58 }
59
60 1;
61
62
63 __END__
64
65 =head1 NAME
66
67 Pod::Simple::Debug -- put Pod::Simple into trace/debug mode
68
69 =head1 SYNOPSIS
70
71  use Pod::Simple::Debug (5);  # or some integer
72
73 Or:
74
75  my $debuglevel;
76  use Pod::Simple::Debug (\$debuglevel, 0);
77  ...some stuff that uses Pod::Simple to do stuff, but which
78   you don't want debug output from...
79
80  $debug_level = 4;
81  ...some stuff that uses Pod::Simple to do stuff, but which
82   you DO want debug output from...
83
84  $debug_level = 0;
85
86 =head1 DESCRIPTION
87
88 This is an internal module for controlling the debug level (a.k.a. trace
89 level) of Pod::Simple.  This is of interest only to Pod::Simple
90 developers.
91
92
93 =head1 CAVEATS
94
95 Note that you should load this module I<before> loading Pod::Simple (or
96 any Pod::Simple-based class).  If you try loading Pod::Simple::Debug
97 after &Pod::Simple::DEBUG is already defined, Pod::Simple::Debug will
98 throw a fatal error to the effect that
99 "It's too late to call Pod::Simple::Debug".
100
101 Note that the C<use Pod::Simple::Debug (\$x, I<somenum>)> mode will make
102 Pod::Simple (et al) run rather slower, since &Pod::Simple::DEBUG won't
103 be a constant sub anymore, and so Pod::Simple (et al) won't compile with
104 constant-folding.
105
106
107 =head1 GUTS
108
109 Doing this:
110
111   use Pod::Simple::Debug (5);  # or some integer
112
113 is basically equivalent to:
114
115   BEGIN { sub Pod::Simple::DEBUG () {5} }  # or some integer
116   use Pod::Simple ();
117
118 And this:
119
120   use Pod::Simple::Debug (\$debug_level,0);  # or some integer
121
122 is basically equivalent to this:
123
124   my $debug_level;
125   BEGIN { $debug_level = 0 }
126   BEGIN { sub Pod::Simple::DEBUG () { $debug_level }
127   use Pod::Simple ();
128
129 =head1 SEE ALSO
130
131 L<Pod::Simple>
132
133 The article "Constants in Perl", in I<The Perl Journal> issue
134 21.  See L<http://interglacial.com/tpj/21/>
135
136 =head1 SUPPORT
137
138 Questions or discussion about POD and Pod::Simple should be sent to the
139 pod-people@perl.org mail list. Send an empty email to
140 pod-people-subscribe@perl.org to subscribe.
141
142 This module is managed in an open GitHub repository,
143 L<https://github.com/perl-pod/pod-simple/>. Feel free to fork and contribute, or
144 to clone L<git://github.com/perl-pod/pod-simple.git> and send patches!
145
146 Patches against Pod::Simple are welcome. Please send bug reports to
147 <bug-pod-simple@rt.cpan.org>.
148
149 =head1 COPYRIGHT AND DISCLAIMERS
150
151 Copyright (c) 2002 Sean M. Burke.
152
153 This library is free software; you can redistribute it and/or modify it
154 under the same terms as Perl itself.
155
156 This program is distributed in the hope that it will be useful, but
157 without any warranty; without even the implied warranty of
158 merchantability or fitness for a particular purpose.
159
160 =head1 AUTHOR
161
162 Pod::Simple was created by Sean M. Burke <sburke@cpan.org>.
163 But don't bother him, he's retired.
164
165 Pod::Simple is maintained by:
166
167 =over
168
169 =item * Allison Randal C<allison@perl.org>
170
171 =item * Hans Dieter Pearcey C<hdp@cpan.org>
172
173 =item * David E. Wheeler C<dwheeler@cpan.org>
174
175 =back
176
177 =cut