| 1 | #!/usr/bin/perl -w | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | # | ||
| 5 | # FormMagick (c) 2000 Kirrily Robert <skud@infotrope.net> | ||
| 6 | # This software is distributed under the GNU General Public License; see | ||
| 7 | # the file COPYING for details. | ||
| 8 | # | ||
| 9 | # $Id: Sub.pm,v 1.5 2001/10/26 17:23:47 ryanking Exp $ | ||
| 10 | # | ||
| 11 | |||
| 12 | =pod | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | CGI::FormMagick::Sub - Call subs by name. | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use CGI::FormMagick::Sub; | ||
| 21 | my %sub = ( | ||
| 22 | package => $some_package_name, | ||
| 23 | sub => $some_sub_name, | ||
| 24 | args => \@array_of_args, | ||
| 25 | comma_delimited_args => $string_of_comma_delimited_args, | ||
| 26 | ); | ||
| 27 | |||
| 28 | CGI::FormMagick::Sub::exists(%sub) or return undef; | ||
| 29 | return CGI::FormMagick::Sub::call(%sub); | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | (Intended for internal use only.) | ||
| 34 | |||
| 35 | Used for calling subs whose names are dynamically generated. | ||
| 36 | |||
| 37 | =head1 STATIC METHODS | ||
| 38 | |||
| 39 | =head2 exists(...) | ||
| 40 | |||
| 41 | exists() takes a hash with keys "package" and "sub". Returns true if | ||
| 42 | the sub exists, false otherwise. | ||
| 43 | |||
| 44 | =head2 call(...) | ||
| 45 | |||
| 46 | call() takes a hash with keys "package" and "sub", and optional "args" and | ||
| 47 | "comma_delimited_args". The "comma_delimited_args" are split up and | ||
| 48 | pushed into the array of args to be sent to the sub when called. Returns | ||
| 49 | the return of the called sub itself. | ||
| 50 | |||
| 51 | If the sub doesn't exist, it will return undef. If $^W is true, it will | ||
| 52 | also complain. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | package CGI::FormMagick::Sub; | ||
| 57 | |||
| 58 | use Carp; | ||
| 59 | |||
| 60 | =begin testing | ||
| 61 | BEGIN { | ||
| 62 | use_ok 'CGI::FormMagick::Sub'; | ||
| 63 | } | ||
| 64 | |||
| 65 | { | ||
| 66 | package main; | ||
| 67 | sub f { | ||
| 68 | return 'Ok' | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | { | ||
| 73 | package ArbitraryPackage; | ||
| 74 | sub hey { | ||
| 75 | return 'You found me.'; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub with_arg { | ||
| 79 | return $_[0] x 2; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub with_args { | ||
| 83 | my $results = ''; | ||
| 84 | $results .= $_ for (reverse @_); | ||
| 85 | return $results; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | { | ||
| 90 | package Ness::ted; | ||
| 91 | |||
| 92 | sub attack { | ||
| 93 | return 'PK Fire!'; # (Sorry -- Smash Bros. reference. =)) | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | foreach my $expectations ( | ||
| 98 | { | ||
| 99 | expected => 'Ok', | ||
| 100 | call_with => { | ||
| 101 | package => 'main', | ||
| 102 | sub => 'f' | ||
| 103 | } | ||
| 104 | }, { | ||
| 105 | expected => 'You found me.', | ||
| 106 | call_with => { | ||
| 107 | package => 'ArbitraryPackage', | ||
| 108 | sub => 'hey' | ||
| 109 | } | ||
| 110 | }, { | ||
| 111 | expected => 'PK Fire!', | ||
| 112 | call_with => { | ||
| 113 | package => 'Ness::ted', | ||
| 114 | sub => 'attack' | ||
| 115 | } | ||
| 116 | }, { | ||
| 117 | expected => 'RepeatRepeat', | ||
| 118 | call_with => { | ||
| 119 | package => 'ArbitraryPackage', | ||
| 120 | sub => 'with_arg', | ||
| 121 | args => [ 'Repeat' ], | ||
| 122 | } | ||
| 123 | }, { | ||
| 124 | expected => 'Backwards', | ||
| 125 | call_with => { | ||
| 126 | package => 'ArbitraryPackage', | ||
| 127 | sub => 'with_args', | ||
| 128 | args => [ qw(wards Back) ] | ||
| 129 | } | ||
| 130 | }, { | ||
| 131 | expected => 'abc', | ||
| 132 | call_with => { | ||
| 133 | package => 'ArbitraryPackage', | ||
| 134 | sub => 'with_args', | ||
| 135 | args => [ qw(c b a) ] | ||
| 136 | } | ||
| 137 | }, { | ||
| 138 | expected => 'abc', | ||
| 139 | call_with => { | ||
| 140 | package => 'ArbitraryPackage', | ||
| 141 | sub => 'with_args', | ||
| 142 | comma_delimited_args => 'c,b,a' | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | # We could do parsing for this, but we'll defer it until it's needed. | ||
| 147 | # , { | ||
| 148 | # expected => 'OneTwoThree', | ||
| 149 | # call_with => { | ||
| 150 | # package => 'ArbitraryPackage', | ||
| 151 | # sub => 'with_args', | ||
| 152 | # comma_delimited_args => '"Thr,ee","Two","One"' | ||
| 153 | # } | ||
| 154 | # } | ||
| 155 | |||
| 156 | ) { | ||
| 157 | my $expected = $expectations->{expected}; | ||
| 158 | my %call_with = %{$expectations->{call_with}}; | ||
| 159 | my ($package, $sub) = @call_with{qw(package sub)}; | ||
| 160 | |||
| 161 | my $description = "$package\::$sub"; | ||
| 162 | |||
| 163 | if (exists $call_with{args}) { | ||
| 164 | $description .= "('" . join("', '", @{$call_with{args}}) . "')"; | ||
| 165 | } else { | ||
| 166 | $description .= '()'; | ||
| 167 | } | ||
| 168 | |||
| 169 | my $actual = CGI::FormMagick::Sub::call(%call_with); | ||
| 170 | |||
| 171 | is($actual, $expected, $description); | ||
| 172 | } | ||
| 173 | |||
| 174 | =end testing | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub call { | ||
| 179 | my %params = @_; | ||
| 180 | my $package = $params{package} || ''; | ||
| 181 | my $sub_name = $params{sub}; | ||
| 182 | my @args = exists $params{args} ? @{$params{args}} : (); | ||
| 183 | |||
| 184 | if (defined $params{comma_delimited_args}) { | ||
| 185 | push @args, split /,\s*/, $params{comma_delimited_args}; | ||
| 186 | } | ||
| 187 | |||
| 188 | my $sub = get_sub($package, $sub_name); | ||
| 189 | |||
| 190 | if (defined $sub) { | ||
| 191 | return $sub->(@args); | ||
| 192 | } else { | ||
| 193 | carp "FormMagick: Couldn't call '$package\:\:$sub_name'" if $^W; | ||
| 194 | return undef; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | =begin testing | ||
| 199 | |||
| 200 | foreach my $expectations ( | ||
| 201 | { expected => 1, sub => 'f' }, | ||
| 202 | { expected => 0, sub => 'shouldnt_exist' }, | ||
| 203 | { expected => 0, sub => 'shouldnt exist' }, | ||
| 204 | ) { | ||
| 205 | my ($expected, $sub) = @{$expectations}{qw(expected sub)}; | ||
| 206 | my $actual = CGI::FormMagick::Sub::exists( | ||
| 207 | package => 'main', | ||
| 208 | sub => $sub | ||
| 209 | ); | ||
| 210 | |||
| 211 | my $should = 'should' . ($expected ? '' : "n't"); | ||
| 212 | my $description = "sub $sub $should exist."; | ||
| 213 | |||
| 214 | is($expected, $actual, $description); | ||
| 215 | } | ||
| 216 | |||
| 217 | =end testing | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | sub exists { | ||
| 222 | my %params = @_; | ||
| 223 | my ($package, $sub) = @params{qw(package sub)}; | ||
| 224 | |||
| 225 | return defined get_sub($package, $sub) ? 1 : 0; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub get_sub { | ||
| 229 | my ($package, $sub) = @_; | ||
| 230 | no strict 'refs'; # Into the Evil Cavern... mwahahahahaaaaaa | ||
| 231 | my $package_symbols = *{$package . '::'}{HASH}; | ||
| 232 | return $package_symbols->{$sub}; | ||
| 233 | } | ||
| 234 | |||
| 235 | return 1; |