blib/lib/ASP/NextLink.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 120 CC 16 SCAR 21.7
CC
16
Cov
0%
parse_linkfile
12.0
GetPreviousURL
12.0
GetPreviousDescription
12.0
CRAP 8.8
1 #####################################################################
2 #
3 # ASP::NextLink - Perl implementation of the ASP
4 # Content-Linking component
5 #
6 # Author: Tim Hammerquist
7 # Revision: 0.11
8 #
9 #####################################################################
10 #
11 # Copyright 2000 Tim Hammerquist. All rights reserved.
12 #
13 # This file is distributed under the Artistic License.
14 # See http://www.perl.com/language/misc/Artistic.html or
15 # the license that comes with your perl distribution.
16 #
17 # Contact me at cafall@voffice.net with any comments,
18 # flames, queries, suggestions, or general curiosity.
19 #
20 #####################################################################
21 package ASP::NextLink;
22  
23 use strict;
24 use CGI::Carp;
25 use vars qw( $VERSION );
26  
27 $VERSION = '0.11';
28  
29 sub new {
30 my $class = shift;
31 die "Cannot call class method on an object" if ref $class;
32 my $linkfile = $main::Server->MapPath( shift );
33 my $self = {};
34 bless $self, $class;
35 $self->parse_linkfile( $linkfile );
36 $self;
37 }
38  
39 sub parse_linkfile {
40 my ($self, $linkfile, $idx) = (shift, shift, 0);
41 die "Cannot call object method on class" unless ref $self;
42 open LNX, "<$linkfile" or die "Can't open $linkfile: $!\n";
43 while ( <LNX> ) {
44 chomp;
45 if ( /^([^\t]+)\t([^\t]+)\t?.*$/ ) {
46 $idx++;
47 $self->{_url}{$main::Server->MapPath($1)} = $idx;
48 $self->{_idx}{$idx} = [ $1, $2 ];
49 }
50 }
51 close LNX;
52 $self->{_file} = $linkfile;
53 $self->{_count} = $idx;
54 1;
55 }
56  
57 =head1 NAME
58  
59 ASP::NextLink - Perl implementation of the NextLink ASP component
60  
61 =head1 SYNOPSIS
62  
63 require ASP::NextLink;
64 $nl = new ASP::NextLink('linkfile.ext');
65  
66 $current = $nl->GetListIndex;
67 for $idx (1..$nl->GetListCount) {
68 my $url = $nl->GetNthURL($idx);
69 my $desc = $nl->GetNthDescription($idx);
70 if ($idx == $current) {
71 print qq(<A href="$url">$desc</A><BR>);
72 }
73 else {
74 print qq(<B>$desc</B>);
75 }
76 }
77  
78 =head1 DESCRIPTION
79  
80 ASP::NextLink is a Perl implementation of MSWC.NextLink, ASP's
81 content-linking component for use with Apache::ASP.
82  
83 =head1 NOTES
84  
85 ASP::NextLink is NOT functionally equivalent to MSWC.NextLink.
86 Whereas each method of MSWC.NextLink takes a file argument,
87 ASP::NextLink takes a file argument ONLY in the constructor
88 ( ASP::NextLink->new("linkfile") ). new() parses the linkfile
89 given; the information derived from this linkfile is subsequently
90 available only through the object returned by new().
91  
92 Attempts to call object methods on a class and attempts to call
93 class methods on an object will both trigger an exception.
94  
95 However, in the interest of portability of algorithms to ASP::NextLink,
96 indexes passed to the GetNth*() methods remain 1-based, as they are in
97 MSWC.NextLink.
98  
99 =head1 USE
100  
101 =head2 require ASP::NextLink;
102  
103 =head1 METHOD REFERENCE
104  
105 =head2 new( linkfile )
106  
107 The new() class method accepts a virtual or relative path.
108 (Paths handed to new() are run through the $Server->MapPath() method.)
109 new() returns a reference to an ASP::NextLink object.
110  
111 my $linkfile = "/links.txt";
112 my $nl = ASP::NextLink->new( $linkfile );
113  
114 From now we will refer to the object returned by new() as $nl.
115  
116 =cut
117  
118  
119 =head2 GetListCount()
120  
121 Returns the number of links (lines containing tab-separated fields)
122 in link file.
123  
124 my $count = $nl->GetListCount();
125  
126 =cut
127  
128 sub GetListCount {
129 my $self = shift;
130 die "Cannot call object method on class" unless ref $self;
131 $self->{_count};
132 }
133  
134 =head2 GetListIndex()
135  
136 Index of the current page in the link file.
137  
138 =cut
139  
140 sub GetListIndex {
141 my $self = shift;
142 die "Cannot call object method on class" unless ref $self;
143 $self->{_url}{$ENV{SCRIPT_FILENAME}}
144 or die "Current page not found in $self->{_file}";
145 }
146  
147 =head2 GetPreviousURL()
148  
149 URL of the previous page in the link file.
150  
151 =cut
152  
153 sub GetPreviousURL {
154 my $self = shift;
155 die "Cannot call object method on class" unless ref $self;
156 my $idx = $self->GetListIndex - 1;
157 exists( $self->{_idx}{$idx} )
158 ? $self->{_idx}{$idx}[0] : undef;
159 }
160  
161 =head2 GetPreviousDescription()
162  
163 Description of the previous page in the link file.
164  
165 =cut
166  
167 sub GetPreviousDescription {
168 my $self = shift;
169 die "Cannot call object method on class" unless ref $self;
170 my $idx = $self->GetListIndex - 1;
171 exists( $self->{_idx}{$idx} )
172 ? $self->{_idx}{$idx}[1] : undef;
173 }
174  
175 =head2 GetNextURL()
176  
177 URL of the next page in the link file.
178  
179 =cut
180  
181 sub GetNextURL {
182 my $self = shift;
183 die "Cannot call object method on class" unless ref $self;
184 my $idx = $self->GetListIndex + 1;
185 exists( $self->{_idx}{$idx} )
186 ? $self->{_idx}{$idx}[1] : undef;
187 }
188  
189 =head2 GetNextDescription()
190  
191 Description of the next page in the link file.
192  
193 =cut
194  
195 sub GetNextDescription {
196 my $self = shift;
197 die "Cannot call object method on class" unless ref $self;
198 my $idx = $self->GetListIndex + 1;
199 exists( $self->{_idx}{$idx} )
200 ? $self->{_idx}{$idx}[1] : undef;
201 }
202  
203 =head2 GetNthURL( n )
204  
205 URL of the nth page in the link file.
206 NOTE: Index is 1-based, NOT zero-based.
207  
208 =cut
209  
210 sub GetNthURL {
211 my $self = shift;
212 die "Cannot call object method on class" unless ref $self;
213 my $idx = shift;
214 $self->{_idx}{$idx}[0];
215 }
216  
217 =head2 GetNthDescription( n )
218  
219 Description of the nth page in the link file.
220 NOTE: Index is 1-based, NOT zero-based.
221  
222 =cut
223  
224 sub GetNthDescription {
225 my $self = shift;
226 die "Cannot call object method on class" unless ref $self;
227 my $idx = shift;
228 $self->{_idx}{$idx}[1];
229 }
230  
231 =head1 AUTHOR
232  
233 Tim Hammerquist E<lt>F<cafall@voffice.net>E<gt>
234  
235 =head1 HISTORY
236  
237 =over 4
238  
239 =item Version 0.11
240  
241 First functional release
242  
243 =back
244  
245 =head1 SEE ALSO
246  
247 ASP(3)
248  
249 =cut
250 1;
251 __END__