| 1 | # Copyright (c) 1998-2005 by Jonathan Swartz. All rights reserved. | ||||||||||||||
| 2 | # This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 3 | # under the same terms as Perl itself. | ||||||||||||||
| 4 | |||||||||||||||
| 5 | # | ||||||||||||||
| 6 | # Miscellaneous tools used by the other Mason modules. Some of these | ||||||||||||||
| 7 | # admittedly exist in better versions on CPAN but we rewrite them so | ||||||||||||||
| 8 | # as to minimize external package requirements. | ||||||||||||||
| 9 | # | ||||||||||||||
| 10 | |||||||||||||||
| 11 | package HTML::Mason::Tools; | ||||||||||||||
| 12 | 34 | $HTML::Mason::Tools::VERSION = '1.59'; | |||||||||||||
| 13 | 34 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 34 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 34 | ▶ | use Cwd; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 34 | ▶ | use File::Spec; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 34 | ▶ | use HTML::Mason::Exceptions( abbr => [qw(system_error param_error error)] ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | 34 | require Exporter; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 34 | ▶ | use vars qw(@ISA @EXPORT_OK); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | |||||||||||||||
| 24 | 34 | @ISA = qw(Exporter); | |||||||||||||
| 25 | 34 | @EXPORT_OK = qw(can_weaken read_file read_file_ref url_escape paths_eq compress_path mason_canonpath taint_is_on load_pkg pkg_loaded absolute_comp_path checksum); | |||||||||||||
| 26 | |||||||||||||||
| 27 | # Is weaken available? Even under 5.6+, it might not be available on systems w/o a compiler. | ||||||||||||||
| 28 | # | ||||||||||||||
| 29 | BEGIN | ||||||||||||||
| 30 | { | ||||||||||||||
| 31 | 34 | ▶ | require Scalar::Util; | ||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.2
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | 34 | ▶ | my $can_weaken = defined &Scalar::Util::weaken ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | 917 | ▶ | sub can_weaken () { $can_weaken } | ||||||||||||
|
Subroutine
can_weaken: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | # read_file($file, $binmode) | ||||||||||||||
| 39 | # Return contents of file. If $binmode is 1, read in binary mode. | ||||||||||||||
| 40 | # | ||||||||||||||
| 41 | sub read_file | ||||||||||||||
| 42 | { | ||||||||||||||
| 43 | 6 | ▶ | my $fh = _get_reading_handle(@_); | ||||||||||||
|
Subroutine
read_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 44 | 6 | return do {local $/; scalar <$fh>}; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | |||||||||||||||
| 48 | # This routine is just like read_file, except more memory-efficient | ||||||||||||||
| 49 | # and better for large files. Probably not quite as fast. | ||||||||||||||
| 50 | # | ||||||||||||||
| 51 | # Using read_file_ref(), I have verified (in 5.6.1, anyway) that | ||||||||||||||
| 52 | # reading a file consumes only about as much memory as the size of the | ||||||||||||||
| 53 | # file. Using read_file() uses 2x the size of the file. | ||||||||||||||
| 54 | # | ||||||||||||||
| 55 | # Don't go using read() willy-nilly, though, it's usually not worth | ||||||||||||||
| 56 | # the potential bugs. It's easy to mess up the logic. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | sub read_file_ref | ||||||||||||||
| 59 | { | ||||||||||||||
| 60 | 571 | ▶ | my $fh = _get_reading_handle(@_); | ||||||||||||
|
Subroutine
read_file_ref: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 61 | 571 | my ($buffer, $retval) = (''); | |||||||||||||
| 62 | 571 | while (1) { | |||||||||||||
| 63 | # Important to read in chunks - 16KB is a good compromise | ||||||||||||||
| 64 | # between not bloating memory usage and not calling read many | ||||||||||||||
| 65 | # times for small files | ||||||||||||||
| 66 | 1133 | $retval = read $fh, $buffer, 1024 * 16, length($buffer); | |||||||||||||
| 67 | 1133 | ▶ | system_error "read_file_ref: Couldn't read from '$_[0]': $!" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | unless defined $retval; | ||||||||||||||
| 69 | 1133 | ▶ | last if !$retval; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | } | ||||||||||||||
| 71 | 571 | return \$buffer; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | sub _get_reading_handle { | ||||||||||||||
| 75 | 577 | ▶ | my ($file,$binmode) = @_; | ||||||||||||
|
Subroutine
_get_reading_handle: called CC=5 SCAR=16.6
| |||||||||||||||
| 76 | 577 | ▶ | error "read_file: '$file' does not exist" unless -e $file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 577 | ▶ | error "read_file: '$file' is a directory" if (-d _); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 577 | ▶ | open my $fh, "< $file" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | or system_error "read_file: could not open file '$file' for reading: $!"; | ||||||||||||||
| 80 | 577 | ▶ | binmode $fh if $binmode; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 577 | return $fh; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | # | ||||||||||||||
| 85 | # Determines whether two paths are equal, taking into account | ||||||||||||||
| 86 | # case-insensitivity in Windows O/S. | ||||||||||||||
| 87 | # | ||||||||||||||
| 88 | sub paths_eq { | ||||||||||||||
| 89 | 0 | ▶ | return File::Spec->case_tolerant ? (lc($_[0]) eq lc($_[1])) : $_[0] eq $_[1]; | ||||||||||||
|
Subroutine
paths_eq: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | # | ||||||||||||||
| 93 | # Compress a component path into a single, filesystem-friendly | ||||||||||||||
| 94 | # string. Uses URL-like escaping with + instead of %. | ||||||||||||||
| 95 | # | ||||||||||||||
| 96 | sub compress_path | ||||||||||||||
| 97 | { | ||||||||||||||
| 98 | 82 | ▶ | my ($path) = @_; | ||||||||||||
|
Subroutine
compress_path: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 99 | 82 | for ($path) { | |||||||||||||
| 100 | 82 | s@^/@@; | |||||||||||||
| 101 | 82 | s/([^\w\.\-\~])/sprintf('+%02x', ord $1)/eg; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | 82 | return $path; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | # | ||||||||||||||
| 107 | # Return the absolute version of a component path. Handles . and .. | ||||||||||||||
| 108 | # Second argument is directory path to resolve relative paths against. | ||||||||||||||
| 109 | # | ||||||||||||||
| 110 | sub absolute_comp_path | ||||||||||||||
| 111 | { | ||||||||||||||
| 112 | 689 | ▶ | my ($comp_path, $dir_path) = @_; | ||||||||||||
|
Subroutine
absolute_comp_path: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 113 | |||||||||||||||
| 114 | 689 | ▶ | $comp_path = "$dir_path/$comp_path" if $comp_path !~ m@^/@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | 689 | return mason_canonpath($comp_path); | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | |||||||||||||||
| 119 | # | ||||||||||||||
| 120 | # Makes a few fixes to File::Spec::canonpath. Will go away if/when they | ||||||||||||||
| 121 | # accept our patch. | ||||||||||||||
| 122 | # | ||||||||||||||
| 123 | sub mason_canonpath { | ||||||||||||||
| 124 | # Just like File::Spec::canonpath, but we're having trouble | ||||||||||||||
| 125 | # getting a patch through to them. | ||||||||||||||
| 126 | 689 | ▶ | my $path = shift; | ||||||||||||
|
Subroutine
mason_canonpath: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||
| 127 | 689 | $path =~ s|/+|/|g; # xx////yy -> xx/yy | |||||||||||||
| 128 | 689 | $path =~ s|(?:/\.)+/|/|g; # xx/././yy -> xx/yy | |||||||||||||
| 129 | { | ||||||||||||||
| 130 | 689 | ▶ | $path =~ s|^(?:\./)+||s unless $path eq "./"; # ./xx -> xx | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 706 | $path =~ s|^/(?:\.\./)+|/|s; # /../../xx -> xx | |||||||||||||
| 132 | 706 | ▶ | $path =~ s|/\Z(?!\n)|| unless $path eq "/"; # xx/ -> xx | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 706 | ▶ | $path =~ s|/[^/]+/\.\.$|| && redo; # /xx/.. -> / | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | 706 | ▶ | $path =~ s|[^/]+/\.\./|| && redo; # /xx/../yy -> /yy | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | } | ||||||||||||||
| 136 | 689 | return $path; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | # | ||||||||||||||
| 140 | # Determine if package is installed without loading it, by checking | ||||||||||||||
| 141 | # the INC path. | ||||||||||||||
| 142 | # | ||||||||||||||
| 143 | sub pkg_installed | ||||||||||||||
| 144 | { | ||||||||||||||
| 145 | 0 | ▶ | my ($pkg) = @_; | ||||||||||||
|
Subroutine
pkg_installed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 146 | |||||||||||||||
| 147 | 0 | (my $pkgfile = "$pkg.pm") =~ s{::}{/}g; | |||||||||||||
| 148 | 0 | return grep(-f "$_/$pkgfile",@INC); | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | # | ||||||||||||||
| 152 | # Determined if package is loaded by checking for its version. | ||||||||||||||
| 153 | # | ||||||||||||||
| 154 | sub pkg_loaded | ||||||||||||||
| 155 | { | ||||||||||||||
| 156 | 84 | ▶ | my ($pkg) = @_; | ||||||||||||
|
Subroutine
pkg_loaded: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 157 | |||||||||||||||
| 158 | 84 | my $varname = "${pkg}::VERSION"; | |||||||||||||
| 159 | 34 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 160 | 84 | ▶ | return $$varname ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | # | ||||||||||||||
| 164 | # Load package $pkg if not already loaded. Return 1 if file was found | ||||||||||||||
| 165 | # and loaded successfully. When file is not found: if optional second | ||||||||||||||
| 166 | # argument $nf_error is provided, die with that error message, | ||||||||||||||
| 167 | # otherwise return 0. Errors while loading the package are always | ||||||||||||||
| 168 | # passed through as fatal errors. | ||||||||||||||
| 169 | # | ||||||||||||||
| 170 | sub load_pkg { | ||||||||||||||
| 171 | 65 | ▶ | my ($pkg, $nf_error) = @_; | ||||||||||||
|
Subroutine
load_pkg: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||
| 172 | |||||||||||||||
| 173 | 65 | my $file = File::Spec->catfile( split /::/, $pkg ); | |||||||||||||
| 174 | 65 | $file .= '.pm'; | |||||||||||||
| 175 | 65 | ▶ | return 1 if exists $INC{$file}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 176 | |||||||||||||||
| 177 | 4 | ▶ | eval "use $pkg"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 178 | |||||||||||||||
| 179 | 4 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 180 | 1 | ▶ | if ($@ =~ /^Can\'t locate (.*) in \@INC/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 181 | 1 | ▶ | if (defined($nf_error)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 182 | 1 | error sprintf("Can't locate %s in \@INC. %s\n(\@INC contains: %s)", | |||||||||||||
| 183 | $1, $nf_error, join(" ", @INC)); | ||||||||||||||
| 184 | } else { | ||||||||||||||
| 185 | 0 | undef $@; | |||||||||||||
| 186 | 0 | return 0; | |||||||||||||
| 187 | } | ||||||||||||||
| 188 | } else { | ||||||||||||||
| 189 | 0 | error $@; | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | } | ||||||||||||||
| 192 | 3 | return 1; | |||||||||||||
| 193 | } | ||||||||||||||
| 194 | |||||||||||||||
| 195 | # This code seems to be very fragile! Please don't check in changes | ||||||||||||||
| 196 | # unless you've tested it with Perl 5.00503, 5.6.1, and 5.8.0, or at | ||||||||||||||
| 197 | # least tell Dave to run the tests. | ||||||||||||||
| 198 | 34 | my $TaintIsOn; | |||||||||||||
| 199 | sub taint_is_on | ||||||||||||||
| 200 | { | ||||||||||||||
| 201 | 1176 | ▶ | return $TaintIsOn if defined $TaintIsOn; | ||||||||||||
|
Subroutine
taint_is_on: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 202 | 31 | return $TaintIsOn = _taint_is_on(); | |||||||||||||
| 203 | } | ||||||||||||||
| 204 | |||||||||||||||
| 205 | sub _taint_is_on | ||||||||||||||
| 206 | { | ||||||||||||||
| 207 | 31 | ▶ | if ( $] >= 5.008 ) | ||||||||||||
|
Subroutine
_taint_is_on: called CC=5 SCAR=24.5
Branch: 1 branch
| |||||||||||||||
| 208 | { | ||||||||||||||
| 209 | # We have to eval a string because this variable name causes | ||||||||||||||
| 210 | # earlier Perls to not compile at all. | ||||||||||||||
| 211 | 31 | ▶ | return eval '${^TAINT}' ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 212 | } | ||||||||||||||
| 213 | else | ||||||||||||||
| 214 | { | ||||||||||||||
| 215 | 0 | local $^W; | |||||||||||||
| 216 | 0 | ▶ | eval { "+$0$^X" && eval 1 }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 217 | 0 | ▶ | return $@ ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 218 | } | ||||||||||||||
| 219 | } | ||||||||||||||
| 220 | |||||||||||||||
| 221 | sub coerce_to_array | ||||||||||||||
| 222 | { | ||||||||||||||
| 223 | 5 | ▶ | my ($val, $name) = @_; | ||||||||||||
|
Subroutine
coerce_to_array: called CC=4 SCAR=17.9
Pod
documented
| |||||||||||||||
| 224 | |||||||||||||||
| 225 | 5 | ▶ | return ($val) unless ref $val; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 226 | |||||||||||||||
| 227 | 5 | ▶ | if ( UNIVERSAL::isa( $val, 'ARRAY' ) ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 228 | { | ||||||||||||||
| 229 | 5 | return @$val; | |||||||||||||
| 230 | } | ||||||||||||||
| 231 | elsif ( UNIVERSAL::isa( $val, 'HASH' ) ) | ||||||||||||||
| 232 | { | ||||||||||||||
| 233 | 0 | return %$val; | |||||||||||||
| 234 | } | ||||||||||||||
| 235 | |||||||||||||||
| 236 | 0 | param_error "Cannot coerce $val to an array for '$name' parameter"; | |||||||||||||
| 237 | } | ||||||||||||||
| 238 | |||||||||||||||
| 239 | sub coerce_to_hash | ||||||||||||||
| 240 | { | ||||||||||||||
| 241 | 2 | ▶ | my ($val, $name) = @_; | ||||||||||||
|
Subroutine
coerce_to_hash: called CC=4 SCAR=16.4
Pod
documented
| |||||||||||||||
| 242 | |||||||||||||||
| 243 | 2 | ▶ | param_error "Cannot convert a single value to a hash for '$name' parameter" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 244 | unless ref $val; | ||||||||||||||
| 245 | |||||||||||||||
| 246 | 2 | ▶ | if ( UNIVERSAL::isa( $val, 'ARRAY' ) ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 247 | { | ||||||||||||||
| 248 | 0 | return @$val; | |||||||||||||
| 249 | } | ||||||||||||||
| 250 | elsif ( UNIVERSAL::isa( $val, 'HASH' ) ) | ||||||||||||||
| 251 | { | ||||||||||||||
| 252 | 2 | return %$val; | |||||||||||||
| 253 | } | ||||||||||||||
| 254 | |||||||||||||||
| 255 | 0 | param_error "Cannot coerce $val to a hash"; | |||||||||||||
| 256 | } | ||||||||||||||
| 257 | |||||||||||||||
| 258 | # Adler32 algorithm | ||||||||||||||
| 259 | sub checksum { | ||||||||||||||
| 260 | 415 | ▶ | my ($str) = @_; | ||||||||||||
|
Subroutine
checksum: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 261 | |||||||||||||||
| 262 | 415 | my $s1 = 1; | |||||||||||||
| 263 | 415 | my $s2 = 1; | |||||||||||||
| 264 | 415 | for my $c (unpack("C*", $str)) { | |||||||||||||
| 265 | 304000 | $s1 = ($s1 + $c ) % 65521; | |||||||||||||
| 266 | 304000 | $s2 = ($s2 + $s1) % 65521; | |||||||||||||
| 267 | } | ||||||||||||||
| 268 | 415 | return ($s2 << 16) + $s1; | |||||||||||||
| 269 | } | ||||||||||||||
| 270 | |||||||||||||||
| 271 | 34 | 1; | |||||||||||||
| 272 | |||||||||||||||
| 273 | __END__ | ||||||||||||||