Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Carp::Assert.3pm
1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 'br\}
42 .\"
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
44 .ie \n(.g .ds Aq \(aq
45 .el       .ds Aq '
46 .\"
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD.  Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
51 .ie \nF \{\
52 .    de IX
53 .    tm Index:\\$1\t\\n%\t"\\$2"
54 ..
55 .    nr % 0
56 .    rr F
57 .\}
58 .el \{\
59 .    de IX
60 ..
61 .\}
62 .\"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
65 .    \" fudge factors for nroff and troff
66 .if n \{\
67 .    ds #H 0
68 .    ds #V .8m
69 .    ds #F .3m
70 .    ds #[ \f1
71 .    ds #] \fP
72 .\}
73 .if t \{\
74 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
75 .    ds #V .6m
76 .    ds #F 0
77 .    ds #[ \&
78 .    ds #] \&
79 .\}
80 .    \" simple accents for nroff and troff
81 .if n \{\
82 .    ds ' \&
83 .    ds ` \&
84 .    ds ^ \&
85 .    ds , \&
86 .    ds ~ ~
87 .    ds /
88 .\}
89 .if t \{\
90 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
96 .\}
97 .    \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 .    \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 .    \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
112 \{\
113 .    ds : e
114 .    ds 8 ss
115 .    ds o a
116 .    ds d- d\h'-1'\(ga
117 .    ds D- D\h'-1'\(hy
118 .    ds th \o'bp'
119 .    ds Th \o'LP'
120 .    ds ae ae
121 .    ds Ae AE
122 .\}
123 .rm #[ #] #H #V #F C
124 .\" ========================================================================
125 .\"
126 .IX Title "Carp::Assert 3"
127 .TH Carp::Assert 3 "2007-01-05" "perl v5.8.7" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
130 .if n .ad l
131 .nh
132 .SH "NAME"
133 Carp::Assert \- executable comments
134 .SH "SYNOPSIS"
135 .IX Header "SYNOPSIS"
136 .Vb 2
137 \&    # Assertions are on.
138 \&    use Carp::Assert;
139 \&
140 \&    $next_sunrise_time = sunrise();
141 \&
142 \&    # Assert that the sun must rise in the next 24 hours.
143 \&    assert(($next_sunrise_time \- time) < 24*60*60) if DEBUG;
144 \&
145 \&    # Assert that your customer\*(Aqs primary credit card is active
146 \&    affirm {
147 \&        my @cards = @{$customer\->credit_cards};
148 \&        $cards[0]\->is_active;
149 \&    };
150 \&
151 \&
152 \&    # Assertions are off.
153 \&    no Carp::Assert;
154 \&
155 \&    $next_pres = divine_next_president();
156 \&
157 \&    # Assert that if you predict Dan Quayle will be the next president
158 \&    # your crystal ball might need some polishing.  However, since
159 \&    # assertions are off, IT COULD HAPPEN!
160 \&    shouldnt($next_pres, \*(AqDan Quayle\*(Aq) if DEBUG;
161 .Ve
162 .SH "DESCRIPTION"
163 .IX Header "DESCRIPTION"
164 .Vb 3
165 \&    "We are ready for any unforseen event that may or may not 
166 \&    occur."
167 \&        \- Dan Quayle
168 .Ve
169 .PP
170 Carp::Assert is intended for a purpose like the \s-1ANSI\s0 C library
171 assert.h.  If you're already familiar with assert.h, then you can
172 probably skip this and go straight to the \s-1FUNCTIONS\s0 section.
173 .PP
174 Assertions are the explict expressions of your assumptions about the
175 reality your program is expected to deal with, and a declaration of
176 those which it is not.  They are used to prevent your program from
177 blissfully processing garbage inputs (garbage in, garbage out becomes
178 garbage in, error out) and to tell you when you've produced garbage
179 output.  (If I was going to be a cynic about Perl and the user nature,
180 I'd say there are no user inputs but garbage, and Perl produces
181 nothing but...)
182 .PP
183 An assertion is used to prevent the impossible from being asked of
184 your code, or at least tell you when it does.  For example:
185 .PP
186 .Vb 3
187 \&    # Take the square root of a number.
188 \&    sub my_sqrt {
189 \&        my($num) = shift;
190 \&
191 \&        # the square root of a negative number is imaginary.
192 \&        assert($num >= 0);
193 \&
194 \&        return sqrt $num;
195 \&    }
196 .Ve
197 .PP
198 The assertion will warn you if a negative number was handed to your
199 subroutine, a reality the routine has no intention of dealing with.
200 .PP
201 An assertion should also be used as something of a reality check, to
202 make sure what your code just did really did happen:
203 .PP
204 .Vb 3
205 \&    open(FILE, $filename) || die $!;
206 \&    @stuff = <FILE>;
207 \&    @stuff = do_something(@stuff);
208 \&
209 \&    # I should have some stuff.
210 \&    assert(@stuff > 0);
211 .Ve
212 .PP
213 The assertion makes sure you have some \f(CW@stuff\fR at the end.  Maybe the
214 file was empty, maybe \fIdo_something()\fR returned an empty list... either
215 way, the \fIassert()\fR will give you a clue as to where the problem lies,
216 rather than 50 lines down at when you wonder why your program isn't
217 printing anything.
218 .PP
219 Since assertions are designed for debugging and will remove themelves
220 from production code, your assertions should be carefully crafted so
221 as to not have any side-effects, change any variables, or otherwise
222 have any effect on your program.  Here is an example of a bad
223 assertation:
224 .PP
225 .Vb 1
226 \&    assert($error = 1 if $king ne \*(AqHenry\*(Aq);  # Bad!
227 .Ve
228 .PP
229 It sets an error flag which may then be used somewhere else in your
230 program. When you shut off your assertions with the \f(CW$DEBUG\fR flag,
231 \&\f(CW$error\fR will no longer be set.
232 .PP
233 Here's another example of \fBbad\fR use:
234 .PP
235 .Vb 1
236 \&    assert($next_pres ne \*(AqDan Quayle\*(Aq or goto Canada);  # Bad!
237 .Ve
238 .PP
239 This assertion has the side effect of moving to Canada should it fail.
240 This is a very bad assertion since error handling should not be
241 placed in an assertion, nor should it have side-effects.
242 .PP
243 In short, an assertion is an executable comment.  For instance, instead
244 of writing this
245 .PP
246 .Vb 2
247 \&    # $life ends with a \*(Aq!\*(Aq
248 \&    $life = begin_life();
249 .Ve
250 .PP
251 you'd replace the comment with an assertion which \fBenforces\fR the comment.
252 .PP
253 .Vb 2
254 \&    $life = begin_life();
255 \&    assert( $life =~ /!$/ );
256 .Ve
257 .SH "FUNCTIONS"
258 .IX Header "FUNCTIONS"
259 .IP "\fBassert\fR" 4
260 .IX Item "assert"
261 .Vb 2
262 \&    assert(EXPR) if DEBUG;
263 \&    assert(EXPR, $name) if DEBUG;
264 .Ve
265 .Sp
266 assert's functionality is effected by compile time value of the \s-1DEBUG\s0
267 constant, controlled by saying \f(CW\*(C`use Carp::Assert\*(C'\fR or \f(CW\*(C`no
268 Carp::Assert\*(C'\fR.  In the former case, assert will function as below.
269 Otherwise, the assert function will compile itself out of the program.
270 See \*(L"Debugging vs Production\*(R" for details.
271 .Sp
272 Give assert an expression, assert will \fICarp::confess()\fR if that
273 expression is false, otherwise it does nothing.  (\s-1DO\s0 \s-1NOT\s0 use the
274 return value of assert for anything, I mean it... really!).
275 .Sp
276 The error from assert will look something like this:
277 .Sp
278 .Vb 3
279 \&    Assertion failed!
280 \&            Carp::Assert::assert(0) called at prog line 23
281 \&            main::foo called at prog line 50
282 .Ve
283 .Sp
284 Indicating that in the file \*(L"prog\*(R" an assert failed inside the
285 function \fImain::foo()\fR on line 23 and that \fIfoo()\fR was in turn called from
286 line 50 in the same file.
287 .Sp
288 If given a \f(CW$name\fR, \fIassert()\fR will incorporate this into your error message,
289 giving users something of a better idea what's going on.
290 .Sp
291 .Vb 2
292 \&    assert( Dogs\->isa(\*(AqPeople\*(Aq), \*(AqDogs are people, too!\*(Aq ) if DEBUG;
293 \&    # Result \- "Assertion (Dogs are people, too!) failed!"
294 .Ve
295 .IP "\fBaffirm\fR" 4
296 .IX Item "affirm"
297 .Vb 2
298 \&    affirm BLOCK if DEBUG;
299 \&    affirm BLOCK $name if DEBUG;
300 .Ve
301 .Sp
302 Very similar to \fIassert()\fR, but instead of taking just a simple
303 expression it takes an entire block of code and evaluates it to make
304 sure its true.  This can allow more complicated assertions than
305 \&\fIassert()\fR can without letting the debugging code leak out into
306 production and without having to smash together several
307 statements into one.
308 .Sp
309 .Vb 5
310 \&    affirm {
311 \&        my $customer = Customer\->new($customerid);
312 \&        my @cards = $customer\->credit_cards;
313 \&        grep { $_\->is_active } @cards;
314 \&    } "Our customer has an active credit card";
315 .Ve
316 .Sp
317 \&\fIaffirm()\fR also has the nice side effect that if you forgot the \f(CW\*(C`if DEBUG\*(C'\fR
318 suffix its arguments will not be evaluated at all.  This can be nice
319 if you stick \fIaffirm()\fRs with expensive checks into hot loops and other
320 time-sensitive parts of your program.
321 .Sp
322 If the \f(CW$name\fR is left off and your Perl version is 5.6 or higher the
323 \&\fIaffirm()\fR diagnostics will include the code begin affirmed.
324 .IP "\fBshould\fR" 4
325 .IX Item "should"
326 .PD 0
327 .IP "\fBshouldnt\fR" 4
328 .IX Item "shouldnt"
329 .PD
330 .Vb 2
331 \&    should  ($this, $shouldbe)   if DEBUG;
332 \&    shouldnt($this, $shouldntbe) if DEBUG;
333 .Ve
334 .Sp
335 Similar to \fIassert()\fR, it is specially for simple \*(L"this should be that\*(R"
336 or \*(L"this should be anything but that\*(R" style of assertions.
337 .Sp
338 Due to Perl's lack of a good macro system, \fIassert()\fR can only report
339 where something failed, but it can't report \fIwhat\fR failed or \fIhow\fR.
340 \&\fIshould()\fR and \fIshouldnt()\fR can produce more informative error messages:
341 .Sp
342 .Vb 3
343 \&    Assertion (\*(Aqthis\*(Aq should be \*(Aqthat\*(Aq!) failed!
344 \&            Carp::Assert::should(\*(Aqthis\*(Aq, \*(Aqthat\*(Aq) called at moof line 29
345 \&            main::foo() called at moof line 58
346 .Ve
347 .Sp
348 So this:
349 .Sp
350 .Vb 1
351 \&    should($this, $that) if DEBUG;
352 .Ve
353 .Sp
354 is similar to this:
355 .Sp
356 .Vb 1
357 \&    assert($this eq $that) if DEBUG;
358 .Ve
359 .Sp
360 except for the better error message.
361 .Sp
362 Currently, \fIshould()\fR and \fIshouldnt()\fR can only do simple eq and ne tests
363 (respectively).  Future versions may allow regexes.
364 .SH "Debugging vs Production"
365 .IX Header "Debugging vs Production"
366 Because assertions are extra code and because it is sometimes necessary to
367 place them in 'hot' portions of your code where speed is paramount,
368 Carp::Assert provides the option to remove its \fIassert()\fR calls from your
369 program.
370 .PP
371 So, we provide a way to force Perl to inline the switched off \fIassert()\fR
372 routine, thereby removing almost all performance impact on your production
373 code.
374 .PP
375 .Vb 2
376 \&    no Carp::Assert;  # assertions are off.
377 \&    assert(1==1) if DEBUG;
378 .Ve
379 .PP
380 \&\s-1DEBUG\s0 is a constant set to 0.  Adding the 'if \s-1DEBUG\s0' condition on your
381 \&\fIassert()\fR call gives perl the cue to go ahead and remove \fIassert()\fR call from
382 your program entirely, since the if conditional will always be false.
383 .PP
384 .Vb 4
385 \&    # With C<no Carp::Assert> the assert() has no impact.
386 \&    for (1..100) {
387 \&        assert( do_some_really_time_consuming_check ) if DEBUG;
388 \&    }
389 .Ve
390 .PP
391 If \f(CW\*(C`if DEBUG\*(C'\fR gets too annoying, you can always use \fIaffirm()\fR.
392 .PP
393 .Vb 4
394 \&    # Once again, affirm() has (almost) no impact with C<no Carp::Assert>
395 \&    for (1..100) {
396 \&        affirm { do_some_really_time_consuming_check };
397 \&    }
398 .Ve
399 .PP
400 Another way to switch off all asserts, system wide, is to define the
401 \&\s-1NDEBUG\s0 or the \s-1PERL_NDEBUG\s0 environment variable.
402 .PP
403 You can safely leave out the \*(L"if \s-1DEBUG\s0\*(R" part, but then your \fIassert()\fR
404 function will always execute (and its arguments evaluated and time
405 spent).  To get around this, use \fIaffirm()\fR.  You still have the
406 overhead of calling a function but at least its arguments will not be
407 evaluated.
408 .SH "Differences from ANSI C"
409 .IX Header "Differences from ANSI C"
410 \&\fIassert()\fR is intended to act like the function from \s-1ANSI\s0 C fame. 
411 Unfortunately, due to Perl's lack of macros or strong inlining, it's not
412 nearly as unobtrusive.
413 .PP
414 Well, the obvious one is the \*(L"if \s-1DEBUG\s0\*(R" part.  This is cleanest way I could
415 think of to cause each \fIassert()\fR call and its arguments to be removed from
416 the program at compile-time, like the \s-1ANSI\s0 C macro does.
417 .PP
418 Also, this version of assert does not report the statement which
419 failed, just the line number and call frame via Carp::confess.  You
420 can't do \f(CW\*(C`assert(\*(Aq$a == $b\*(Aq)\*(C'\fR because \f(CW$a\fR and \f(CW$b\fR will probably be
421 lexical, and thus unavailable to \fIassert()\fR.  But with Perl, unlike C,
422 you always have the source to look through, so the need isn't as
423 great.
424 .SH "EFFICIENCY"
425 .IX Header "EFFICIENCY"
426 With \f(CW\*(C`no Carp::Assert\*(C'\fR (or \s-1NDEBUG\s0) and using the \f(CW\*(C`if DEBUG\*(C'\fR suffixes
427 on all your assertions, Carp::Assert has almost no impact on your
428 production code.  I say almost because it does still add some load-time
429 to your code (I've tried to reduce this as much as possible).
430 .PP
431 If you forget the \f(CW\*(C`if DEBUG\*(C'\fR on an \f(CW\*(C`assert()\*(C'\fR, \f(CW\*(C`should()\*(C'\fR or
432 \&\f(CW\*(C`shouldnt()\*(C'\fR, its arguments are still evaluated and thus will impact
433 your code.  You'll also have the extra overhead of calling a
434 subroutine (even if that subroutine does nothing).
435 .PP
436 Forgetting the \f(CW\*(C`if DEBUG\*(C'\fR on an \f(CW\*(C`affirm()\*(C'\fR is not so bad.  While you
437 still have the overhead of calling a subroutine (one that does
438 nothing) it will \fBnot\fR evaluate its code block and that can save
439 alot.
440 .PP
441 Try to remember the \fBif \s-1DEBUG\s0\fR.
442 .SH "ENVIRONMENT"
443 .IX Header "ENVIRONMENT"
444 .IP "\s-1NDEBUG\s0" 4
445 .IX Item "NDEBUG"
446 Defining \s-1NDEBUG\s0 switches off all assertions.  It has the same effect
447 as changing \*(L"use Carp::Assert\*(R" to \*(L"no Carp::Assert\*(R" but it effects all
448 code.
449 .IP "\s-1PERL_NDEBUG\s0" 4
450 .IX Item "PERL_NDEBUG"
451 Same as \s-1NDEBUG\s0 and will override it.  Its provided to give you
452 something which won't conflict with any C programs you might be
453 working on at the same time.
454 .SH "BUGS, CAVETS and other MUSINGS"
455 .IX Header "BUGS, CAVETS and other MUSINGS"
456 .ie n .SS "Conflicts with ""POSIX.pm"""
457 .el .SS "Conflicts with \f(CWPOSIX.pm\fP"
458 .IX Subsection "Conflicts with POSIX.pm"
459 The \f(CW\*(C`POSIX\*(C'\fR module exports an \f(CW\*(C`assert\*(C'\fR routine which will conflict with \f(CW\*(C`Carp::Assert\*(C'\fR if both are used in the same namespace.  If you are using both together, prevent \f(CW\*(C`POSIX\*(C'\fR from exporting like so:
460 .PP
461 .Vb 2
462 \&    use POSIX ();
463 \&    use Carp::Assert;
464 .Ve
465 .PP
466 Since \f(CW\*(C`POSIX\*(C'\fR exports way too much, you should be using it like that anyway.
467 .ie n .SS """affirm"" and $^S"
468 .el .SS "\f(CWaffirm\fP and \f(CW$^S\fP"
469 .IX Subsection "affirm and $^S"
470 \&\fIaffirm()\fR mucks with the expression's caller and it is run in an eval
471 so anything that checks $^S will be wrong.
472 .ie n .SS """shouldn\*(Aqt"""
473 .el .SS "\f(CWshouldn\*(Aqt\fP"
474 .IX Subsection "shouldnt"
475 Yes, there is a \f(CW\*(C`shouldn\*(Aqt\*(C'\fR routine.  It mostly works, but you \fBmust\fR
476 put the \f(CW\*(C`if DEBUG\*(C'\fR after it.
477 .ie n .SS "missing ""if DEBUG"""
478 .el .SS "missing \f(CWif DEBUG\fP"
479 .IX Subsection "missing if DEBUG"
480 It would be nice if we could warn about missing \f(CW\*(C`if DEBUG\*(C'\fR.
481 .SH "SEE ALSO"
482 .IX Header "SEE ALSO"
483 assertions is a new module available in 5.9.0 which provides assertions which can be enabled/disabled at compile time for real, no \f(CW\*(C`if DEBUG\*(C'\fR necessary.
484 .SH "COPYRIGHT"
485 .IX Header "COPYRIGHT"
486 Copyright 2001\-2007 by Michael G Schwern <schwern@pobox.com>.
487 .PP
488 This program is free software; you can redistribute it and/or 
489 modify it under the same terms as Perl itself.
490 .PP
491 See \fIhttp://dev.perl.org/licenses/\fR
492 .SH "AUTHOR"
493 .IX Header "AUTHOR"
494 Michael G Schwern <schwern@pobox.com>