| 1 | ############################################################################### | ||
| 2 | # XML::Template::Subroutine | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Subroutine; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use Exporter; | ||
| 14 | use POSIX qw(strftime); | ||
| 15 | |||
| 16 | use vars qw(@ISA @EXPORT_OK $AUTOLOAD); | ||
| 17 | |||
| 18 | |||
| 19 | @ISA = qw(Exporter); | ||
| 20 | @EXPORT_OK = qw(defined set_date_vars); | ||
| 21 | |||
| 22 | |||
| 23 | =pod | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | XML::Template::Subroutine - The default XML::Template subroutine handling | ||
| 28 | module. | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | XML::Template supports calling subroutines on variables (e.g., | ||
| 33 | C<${varname}.subname (params)>). You can associate a Perl module with a | ||
| 34 | subroutine in the XML::Template configuration file (see | ||
| 35 | L<XML::Template::Config>. Whenever the subroutine is encountered, the | ||
| 36 | module will be loaded and the method with the same name as the | ||
| 37 | XML::Template subroutine will be called with the following parameters: the | ||
| 38 | name of the variable the subroutine is being called on, the variable's | ||
| 39 | value, an array of additional parameters to the XML::Template subroutine. | ||
| 40 | If no Perl module is associated with a subroutine, the subroutine is | ||
| 41 | called from this module. Tpically, these subroutines are not called | ||
| 42 | directly but from the method C<subroutine> in L<XML::Template::Process>. | ||
| 43 | |||
| 44 | =head1 XML::Template SUBROUTINES | ||
| 45 | |||
| 46 | =head2 defined | ||
| 47 | |||
| 48 | Returns true if the variable is defined. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub defined { | ||
| 53 | my $class = shift; | ||
| 54 | my $process = shift; | ||
| 55 | my ($var, $value) = @_; | ||
| 56 | |||
| 57 | return CORE::defined $value; | ||
| 58 | } | ||
| 59 | |||
| 60 | =pod | ||
| 61 | |||
| 62 | =head2 set_date_vars | ||
| 63 | |||
| 64 | This subroutine is used to set various date related variables in the | ||
| 65 | current variable context. It should be called on a variable whose value | ||
| 66 | is in the MySQL datetime format. It take an addition parameter, | ||
| 67 | C<prefix>, which is prepended to each date variable set. Variables are | ||
| 68 | set for the year, month, day, hour, minute, and second. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub set_date_vars { | ||
| 73 | my $class = shift; | ||
| 74 | my $process = shift; | ||
| 75 | my ($var, $value, $prefix) = @_; | ||
| 76 | |||
| 77 | my $vars = $process->{_vars}; | ||
| 78 | |||
| 79 | $value =~ /^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/; | ||
| 80 | $vars->set ($prefix . "_year", $1); | ||
| 81 | $vars->set ($prefix . "_month", $2); | ||
| 82 | $vars->set ($prefix . "_day", $3); | ||
| 83 | $vars->set ($prefix . "_hour", $4); | ||
| 84 | $vars->set ($prefix . "_minute", $5); | ||
| 85 | $vars->set ($prefix . "_second", $6); | ||
| 86 | |||
| 87 | return ''; | ||
| 88 | } | ||
| 89 | |||
| 90 | =pod | ||
| 91 | |||
| 92 | =head2 format_date | ||
| 93 | |||
| 94 | This subroutine returns a date in the format specified by the additional | ||
| 95 | parameter C<format>. It currently only supports variable values in the | ||
| 96 | MySQL datetime format. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub format_date { | ||
| 101 | my $class = shift; | ||
| 102 | my $process = shift; | ||
| 103 | my ($var, $value, $format) = @_; | ||
| 104 | |||
| 105 | if ($value =~ /^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/) { | ||
| 106 | my $year = $1 - 1900; | ||
| 107 | my $mon = $2 - 1; | ||
| 108 | return strftime ($format, $6, $5, $4, $3, $mon, $year); | ||
| 109 | } | ||
| 110 | |||
| 111 | return ''; | ||
| 112 | } | ||
| 113 | |||
| 114 | =pod | ||
| 115 | |||
| 116 | =head2 replace | ||
| 117 | |||
| 118 | This subroutine replaces a substring of the value identified by a regular | ||
| 119 | expression with another string. The first additional parameter is the | ||
| 120 | regular expression, the second is the replacement string. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub replace { | ||
| 125 | my $class = shift; | ||
| 126 | my $process = shift; | ||
| 127 | my ($var, $value, $pattern, $replace) = @_; | ||
| 128 | |||
| 129 | $value =~ s/$pattern/$replace/; | ||
| 130 | $process->{_vars}->set ($var, $value); | ||
| 131 | |||
| 132 | return ''; | ||
| 133 | } | ||
| 134 | |||
| 135 | =pod | ||
| 136 | |||
| 137 | =head2 encrypt | ||
| 138 | |||
| 139 | This subroutine encrypts the value and returns the result. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub encrypt { | ||
| 144 | my $class = shift; | ||
| 145 | my $process = shift; | ||
| 146 | my ($var, $value) = @_; | ||
| 147 | |||
| 148 | srand; | ||
| 149 | my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/'); | ||
| 150 | my $salt = join '', @salt_chars[rand 64, rand 64]; | ||
| 151 | my $encrypted = crypt ($value, $salt); | ||
| 152 | |||
| 153 | return $encrypted; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub AUTOLOAD { | ||
| 157 | my $class = shift; | ||
| 158 | my $process = shift; | ||
| 159 | my ($var, $value, @params) = @_; | ||
| 160 | |||
| 161 | if ($AUTOLOAD !~ /DESTROY$/) { | ||
| 162 | my $method = $AUTOLOAD; | ||
| 163 | $method =~ s/.*:://; | ||
| 164 | |||
| 165 | return defined $value ? $value->$method (@params) : undef; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | =pod | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Jonathan Waxman | ||
| 174 | <jowaxman@bbl.med.upenn.edu> | ||
| 175 | |||
| 176 | =head1 COPYRIGHT | ||
| 177 | |||
| 178 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 179 | All rights reserved. | ||
| 180 | |||
| 181 | This program is free software; you can redistribute it and/or | ||
| 182 | modify it under the same terms as Perl itself. | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | |||
| 187 | 1; |