| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package HTML::ScriptLoader; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
51015
|
use 5.008008; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
43
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
34
|
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
49
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
|
8
|
1
|
|
|
1
|
|
1720
|
use AutoLoader qw(AUTOLOAD); |
|
|
1
|
|
|
|
|
1999
|
|
|
|
1
|
|
|
|
|
5
|
|
|
9
|
1
|
|
|
1
|
|
42
|
use Carp; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
103
|
|
|
10
|
1
|
|
|
1
|
|
1184
|
use URI::Escape; |
|
|
1
|
|
|
|
|
1821
|
|
|
|
1
|
|
|
|
|
930
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
HTML::ScriptLoader - Perl extension for loading scripts on a web page |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use HTML::ScriptLoader; |
|
19
|
|
|
|
|
|
|
my $scripts = HTML::ScriptLoader->new( |
|
20
|
|
|
|
|
|
|
{ |
|
21
|
|
|
|
|
|
|
'other-script' => { |
|
22
|
|
|
|
|
|
|
'uri' => 'http://example.com/other-script.js' |
|
23
|
|
|
|
|
|
|
}, |
|
24
|
|
|
|
|
|
|
'myscript' => { |
|
25
|
|
|
|
|
|
|
'uri' => '/static/js/myscript.js', |
|
26
|
|
|
|
|
|
|
'deps' => ['other-script'], |
|
27
|
|
|
|
|
|
|
'params' => { |
|
28
|
|
|
|
|
|
|
'apikey' => 'very-secret', |
|
29
|
|
|
|
|
|
|
}, |
|
30
|
|
|
|
|
|
|
}, |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$scripts->add_script('myscript'); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$ttvars->{'javascripts'} = $scripts->scripts; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# In your templates (TT) |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
[% FOREACH js IN javascripts %] |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
[% END %] |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
This package handles script loading with dependency support. |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
The available scripts can be setup in a configuration file and added on |
|
52
|
|
|
|
|
|
|
runtime. When a script is needed, you call on L, and the script |
|
53
|
|
|
|
|
|
|
and all its dependencies will be loaded, in order of dependency. |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Recursive dependencies are not allowed and will throw an exception. |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 EXPORT |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
None by default. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
|
66
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
|
67
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# This allows declaration use HTML::ScriptLoader ':all'; |
|
70
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
|
71
|
|
|
|
|
|
|
# will save memory. |
|
72
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
) ] ); |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
our @EXPORT = qw( |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
); |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
our $VERSION = '1.01'; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 METHODS |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 new(C<\%attrs>) |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Constructor. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
It will return the reference to a blessed object of this package. |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
The C<\%attrs> parameter should contain a HASHREF |
|
94
|
|
|
|
|
|
|
with the scripts you want to use for your site. |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub new { |
|
99
|
1
|
|
|
1
|
1
|
25
|
my ( $class, $scripts ) = @_; |
|
100
|
1
|
|
33
|
|
|
8
|
$class = ref $class || $class; |
|
101
|
1
|
|
50
|
|
|
3
|
$scripts ||= {}; |
|
102
|
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
4
|
my %self = ( |
|
104
|
|
|
|
|
|
|
'_scripts' => $scripts, |
|
105
|
|
|
|
|
|
|
'_loaded' => [], |
|
106
|
|
|
|
|
|
|
); |
|
107
|
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
5
|
my $self = bless \%self, $class; |
|
109
|
1
|
|
|
|
|
3
|
return $self; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 add_script(C<@scripts>) |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This prepares a script, or a list of scripts, for use on a web page. |
|
115
|
|
|
|
|
|
|
It will resolve all dependencies, ready for use in a template. |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub add_script { |
|
120
|
3
|
|
|
3
|
1
|
8
|
my ( $self, @scripts ) = @_; |
|
121
|
|
|
|
|
|
|
|
|
122
|
3
|
|
|
|
|
6
|
for my $script (@scripts) { |
|
123
|
2
|
50
|
|
|
|
7
|
next if $self->is_loaded($script); |
|
124
|
|
|
|
|
|
|
|
|
125
|
2
|
50
|
|
|
|
7
|
croak("No script named $script exists") |
|
126
|
|
|
|
|
|
|
unless $self->available->{$script}; |
|
127
|
|
|
|
|
|
|
|
|
128
|
2
|
|
|
|
|
7
|
my @deps = $self->_find_dependencies($script); |
|
129
|
|
|
|
|
|
|
|
|
130
|
1
|
|
|
|
|
6
|
croak( |
|
131
|
|
|
|
|
|
|
"We do not allow recursive dependencies between scripts" |
|
132
|
2
|
50
|
|
|
|
18
|
) if grep { $script eq $_ } @deps; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Add dependencies |
|
135
|
2
|
|
|
|
|
11
|
$self->add_script(@deps); |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Add it to the loaded stack |
|
138
|
2
|
|
|
|
|
3
|
push @{ $self->{'_loaded'} }, $script; |
|
|
2
|
|
|
|
|
9
|
|
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 is_loaded(C<$script>) |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
This method checks if a script has already been loaded. |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub is_loaded { |
|
149
|
2
|
|
|
2
|
1
|
5
|
my ( $self, $script ) = @_; |
|
150
|
|
|
|
|
|
|
|
|
151
|
2
|
50
|
|
|
|
7
|
return unless $self->loaded; |
|
152
|
|
|
|
|
|
|
|
|
153
|
0
|
0
|
|
|
|
0
|
croak "Missing script parameter" |
|
154
|
|
|
|
|
|
|
unless $script; |
|
155
|
|
|
|
|
|
|
|
|
156
|
0
|
0
|
|
|
|
0
|
return 1 if grep { $script eq $_ } $self->loaded; |
|
|
0
|
|
|
|
|
0
|
|
|
157
|
0
|
|
|
|
|
0
|
return; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 _find_dependencies(C<$script>) |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This takes the name of a script as a parameter and looks up all dependencies |
|
163
|
|
|
|
|
|
|
that this script depends on. |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
The list of dependencies is returned. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=cut |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub _find_dependencies { |
|
170
|
2
|
|
|
2
|
|
3
|
my ( $self, $script ) = @_; |
|
171
|
|
|
|
|
|
|
|
|
172
|
2
|
|
|
|
|
4
|
my $scriptconf = $self->available->{$script}; |
|
173
|
2
|
100
|
|
|
|
3
|
my @deps = @{ $scriptconf->{'deps'} || [] }; |
|
|
2
|
|
|
|
|
13
|
|
|
174
|
|
|
|
|
|
|
|
|
175
|
2
|
|
|
|
|
5
|
return @deps; |
|
176
|
|
|
|
|
|
|
} |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 scripts |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
This method returns an ARRAYREF of all scripts that have been loaded. |
|
181
|
|
|
|
|
|
|
It will add all the query parameters to the URI's. |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub scripts { |
|
186
|
4
|
|
|
4
|
1
|
497
|
my ( $self ) = @_; |
|
187
|
|
|
|
|
|
|
|
|
188
|
4
|
|
|
|
|
6
|
my @scripts = (); |
|
189
|
4
|
|
|
|
|
11
|
for my $scriptname ($self->loaded) { |
|
190
|
6
|
|
|
|
|
11
|
my $script = $self->available->{$scriptname}; |
|
191
|
|
|
|
|
|
|
|
|
192
|
6
|
|
|
|
|
12
|
$script->{'name'} = $scriptname; |
|
193
|
6
|
|
|
|
|
8
|
my $url = $script->{'uri'}; |
|
194
|
6
|
100
|
|
|
|
7
|
my %params = %{ $script->{'params'} || {} }; |
|
|
6
|
|
|
|
|
29
|
|
|
195
|
|
|
|
|
|
|
|
|
196
|
6
|
|
|
|
|
13
|
my @params = (); |
|
197
|
6
|
|
|
|
|
40
|
while (my ($key, $val) = each %params) { |
|
198
|
3
|
|
|
|
|
12
|
push @params, sprintf("%s=%s", |
|
199
|
|
|
|
|
|
|
uri_escape($key), |
|
200
|
|
|
|
|
|
|
uri_escape($val) |
|
201
|
|
|
|
|
|
|
); |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
|
|
204
|
6
|
|
|
|
|
120
|
my $strparams = join '&', @params; |
|
205
|
|
|
|
|
|
|
|
|
206
|
6
|
100
|
|
|
|
16
|
$url = "$url?$strparams" |
|
207
|
|
|
|
|
|
|
if $strparams; |
|
208
|
|
|
|
|
|
|
|
|
209
|
6
|
|
|
|
|
10
|
$script->{'url'} = $url; |
|
210
|
6
|
|
|
|
|
17
|
push @scripts, $script; |
|
211
|
|
|
|
|
|
|
} |
|
212
|
|
|
|
|
|
|
|
|
213
|
4
|
|
|
|
|
19
|
return \@scripts; |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head2 loaded |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
This is an accessor for an ARRAY of scripts that has been loaded and ready |
|
219
|
|
|
|
|
|
|
for use on a web site, with all dependencies resolved. |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub loaded { |
|
224
|
6
|
|
|
6
|
1
|
8
|
my ( $self ) = @_; |
|
225
|
6
|
|
|
|
|
7
|
return @{ $self->{'_loaded'} }; |
|
|
6
|
|
|
|
|
29
|
|
|
226
|
|
|
|
|
|
|
} |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head2 available |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
This is an accessor for a HASHREF of scripts that is available for this object. |
|
231
|
|
|
|
|
|
|
Dependencies have not been resolved. |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=cut |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
sub available { |
|
236
|
11
|
|
|
11
|
1
|
754
|
my ( $self ) = @_; |
|
237
|
11
|
|
|
|
|
34
|
return $self->{'_scripts'}; |
|
238
|
|
|
|
|
|
|
} |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head1 AUTHOR |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Knut-Olav Hoven, Eknut-olav@hoven.wsE |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Copyright (C) 2008 by Knut-Olav Hoven |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
250
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
|
251
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=cut |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
1; |
|
256
|
|
|
|
|
|
|
__END__ |