line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package constant::def; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
28667
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
77
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
51
|
|
5
|
2
|
|
|
2
|
|
12
|
use constant (); |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
479
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
constant::def - Perl pragma to declare previously undeclared constants |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 0.01 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=cut |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Define compile-time constant only if it wasn't previously defined elsewhere. |
22
|
|
|
|
|
|
|
The main reason is to use for debugging constants, since there is no way to change the value, except by editing the source |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# common way: (redefine may be done only in source file) |
25
|
|
|
|
|
|
|
use constant DEBUG => ...; |
26
|
|
|
|
|
|
|
# or |
27
|
|
|
|
|
|
|
BEGIN { *DEBUG = sub () { ... } } |
28
|
|
|
|
|
|
|
# or |
29
|
|
|
|
|
|
|
sub DEBUG () { ... } |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
################ |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# complex way: redefine works, if done before use of module |
34
|
|
|
|
|
|
|
# in main.pl |
35
|
|
|
|
|
|
|
BEGIN { *My::Module::Debug = sub () { 1 }; } |
36
|
|
|
|
|
|
|
use My::Module; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# in My/Module.pm |
39
|
|
|
|
|
|
|
BEGIN { defined &DEBUG or do { my $debug = $ENV{MY_MODULE_DEBUG} || 0; *DEBUG = sub () { $debug } } } |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
################ |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# using this distribution |
44
|
|
|
|
|
|
|
# redefine works, if done before use of module |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# in main.pl |
47
|
|
|
|
|
|
|
use constant::abs 'My::Module::DEBUG' => 1; |
48
|
|
|
|
|
|
|
use My::Module; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# in My/Module.pm |
51
|
|
|
|
|
|
|
use constant::def DEBUG => $ENV{MY_MODULE_DEBUG} || 0; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Syntax is fully compatible with C |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub import { |
58
|
4
|
|
|
4
|
|
67
|
my $class = shift; |
59
|
4
|
100
|
|
|
|
20
|
return unless @_; |
60
|
3
|
|
|
|
|
6
|
my $pkg = caller; |
61
|
3
|
|
|
|
|
6
|
my $multiple = ref $_[0]; |
62
|
3
|
100
|
|
|
|
8
|
if (ref $_[0] eq 'HASH') { |
63
|
1
|
|
|
|
|
2
|
for ( keys %{$_[0]} ) { |
|
1
|
|
|
|
|
5
|
|
64
|
1
|
50
|
|
|
|
2
|
delete $_[0]{$_} if defined &{ $pkg . '::' . $_ } |
|
1
|
|
|
|
|
9
|
|
65
|
|
|
|
|
|
|
} |
66
|
1
|
50
|
|
|
|
3
|
return unless %{$_[0]}; |
|
1
|
|
|
|
|
5
|
|
67
|
|
|
|
|
|
|
} else { |
68
|
2
|
100
|
|
|
|
6
|
if (defined $_[0]) { |
69
|
1
|
50
|
|
|
|
3
|
return if defined &{ $pkg . '::' . $_[0] }; |
|
1
|
|
|
|
|
39
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
2
|
|
|
|
|
4
|
unshift @_,'constant'; |
73
|
2
|
|
|
|
|
2186
|
goto &constant::import; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 SEE ALSO |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
L, L |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 AUTHOR |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Mons Anderson, C<< >> |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 BUGS |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
87
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
88
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Copyright 2009 Mons Anderson. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
95
|
|
|
|
|
|
|
under the same terms as Perl itself. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; # End of constant::def |