line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Devel::UseFromCommandLineOnly; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
49070
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
82
|
|
4
|
|
|
|
|
|
|
#use warnings; |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
11
|
use Carp qw(croak confess); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
161
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
9
|
use vars qw($VERSION); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
425
|
|
9
|
|
|
|
|
|
|
$VERSION = "1.00"; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Devel::UseFromCommandLineOnly - use a module from the command line only |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
package Foo; |
18
|
|
|
|
|
|
|
use base qw(Devel::UseFromCommandLineOnly); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# it's okay to use from the command line |
21
|
|
|
|
|
|
|
# these examples will work |
22
|
|
|
|
|
|
|
perl -MFoo -E 'say "This will work!"' |
23
|
|
|
|
|
|
|
perl -MFoo -E 'say "This will work!"' |
24
|
|
|
|
|
|
|
echo 'use Foo; use 5.010; say "This will work!' | perl |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# but not from a file or module |
27
|
|
|
|
|
|
|
# these examples will die |
28
|
|
|
|
|
|
|
echo "use Foo;" > /tmp/foo.pl; perl /tmp/foo.pl |
29
|
|
|
|
|
|
|
echo "package Bar; use Foo;" > /tmp/Bar.pm; perl -I/tmp -MBar |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DESCRIPTION |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
This module prevents you from loading any subclass of it from anywhere but |
34
|
|
|
|
|
|
|
the command line. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This is most useful for writing development tools that monkeypatch other people's |
37
|
|
|
|
|
|
|
code. These hacks are fine to enable from the command line during development, |
38
|
|
|
|
|
|
|
but you wouldn't want to allow anyone to perminatly install them in any code that |
39
|
|
|
|
|
|
|
they could ship as the hacks could break at any point. See L |
40
|
|
|
|
|
|
|
as a good example of this. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
To use it you simply subclass the module: |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
package Foo; |
45
|
|
|
|
|
|
|
use base qw(Devel::UseFromCommandLineOnly); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
This exposes an C routine that checks if you're calling it from a |
48
|
|
|
|
|
|
|
script or module or from the command line and throws an exception if it's |
49
|
|
|
|
|
|
|
the former. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub import { |
54
|
2
|
|
|
2
|
|
23
|
my $pkg = shift; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# don't fire when the module that's using this uses it |
57
|
2
|
100
|
|
|
|
19
|
if ($pkg eq "Devel::UseFromCommandLineOnly") { |
58
|
1
|
|
|
|
|
8
|
return; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# process all other import arguments |
62
|
1
|
|
|
|
|
3
|
foreach (@_) { |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# skip the checks if they're disabled |
65
|
1
|
50
|
|
|
|
5
|
if ($_ eq "disable_command_line_checks") { |
66
|
1
|
|
|
|
|
1565
|
return; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# go bang if we didn't understand the import argument |
70
|
0
|
|
|
|
|
|
croak "Invalid import argument to $pkg: $_" |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# panic if this isn't a "-" or "-e" invocation |
75
|
0
|
|
|
|
|
|
my ($package, $filename, $line) = caller; |
76
|
0
|
0
|
0
|
|
|
|
unless ($filename eq "-e" || $filename eq "-" || $line == 0) { |
|
|
|
0
|
|
|
|
|
77
|
0
|
|
|
|
|
|
croak "Invalid use of $pkg in '$filename' at line $line; This module can only be loaded from the command line"; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 Disabling this module's functionality |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
The one place that subclasses of this module will be needed to be loaded |
86
|
|
|
|
|
|
|
from within a script that is testing that subclass. In this case |
87
|
|
|
|
|
|
|
it's possible to override this module's behavior: |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
#!/usr/bin/perl |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
use Test::More tests => 1; |
92
|
|
|
|
|
|
|
use Foo qw(disable_command_line_checks); |
93
|
|
|
|
|
|
|
isa_ok(Foo->new(), "Foo"); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
End users should NEVER EVER DO THIS. Or, if they do, they're playing |
96
|
|
|
|
|
|
|
with fire and deserve to get burnt... |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 AUTHOR |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Written by Mark Fowler Emark@twoshortplanks.comE |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Copryright Mark Fowler 2009. All Rights Reserved. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This program is free software; you can redistribute it |
105
|
|
|
|
|
|
|
and/or modify it under the same terms as Perl itself. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 BUGS |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
None known. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Please see http://www.twoshortplanks.com/project/devel-usefromcommandline for |
112
|
|
|
|
|
|
|
details of how to submit bugs, access the source control for |
113
|
|
|
|
|
|
|
this project, and contact the author. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 SEE ALSO |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
L |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |