1 package Catalyst::Controller::CGIBin;
8 use File::Slurp 'slurp';
9 use File::Find::Rule ();
11 use Catalyst::Exception ();
12 use File::Spec::Functions 'splitdir';
14 use parent 'Catalyst::Controller::WrapCGI';
18 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
26 our $VERSION = '0.001';
32 package MyApp::Controller::Foo;
34 use parent qw/Catalyst::Controller::CGIBin/;
36 # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
37 sub dongs : Local Args(0) {
39 $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
46 username_field username # used for REMOTE_USER env var
55 Dispatches to executable CGI files in root/cgi-bin for /cgi-bin/ paths.
57 CGI paths are converted into action names using cgi_action (below.)
59 A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
60 C<foo/CGI_hlagh_bar_cgi>, for controller Foo, with the C</>s converted to C<_>s
61 and prepended with C<CGI_>, as well as all non-word characters converted to
62 C<_>s. This is because L<Catalyst> action names can't have non-word characters
65 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
66 module for configuration information.
70 sub register_actions {
71 my ($self, $app) = @_;
75 my $cgi_bin = $app->path_to('root', 'cgi-bin');
78 Catalyst::Exception->throw(
79 message => 'You have no root/cgi-bin directory'
82 my $namespace = $self->action_namespace($app);
84 my $class = ref $self || $self;
86 for my $file (File::Find::Rule->executable->file->in(".")) {
88 my $code = do { no warnings; eval 'sub { '.slurp($file).' }' };
94 $cgi = sub { system "$cgi_bin/$file" };
99 $app->log->info("Registering root/cgi_bin/$file as a $type CGI.")
102 my $action_name = $self->cgi_action($file);
103 my $path = join '/' => splitdir($file);
104 my $reverse = $namespace ? "$namespace/$action_name" : $action_name;
105 my $attrs = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
108 my ($controller, $context) = @_;
109 $controller->cgi_to_response($context, $cgi)
112 my $action = $self->create_action(
113 name => $action_name,
116 namespace => $namespace,
121 $app->dispatcher->register($app, $action);
126 $self->next::method($app, @_);
131 =head2 $self->cgi_action($cgi_path)
133 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
134 the action name it is registered as.
139 my ($self, $cgi) = @_;
141 my $action_name = 'CGI_' . join '_' => splitdir($cgi);
142 $action_name =~ s/\W/_/g;
149 Rafael Kitover, C<< <rkitover at cpan.org> >>
153 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
154 rt.cpan.org>, or through the web interface at
155 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
156 I will be notified, and then you'll automatically be notified of progress on
157 your bug as I make changes.
165 =item * RT: CPAN's request tracker
167 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
169 =item * AnnoCPAN: Annotated CPAN documentation
171 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
175 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
179 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
183 =head1 COPYRIGHT & LICENSE
185 Copyright (c) 2008 Rafael Kitover
187 This program is free software; you can redistribute it and/or modify it
188 under the same terms as Perl itself.
192 1; # End of Catalyst::Controller::CGIBin
194 # vim: expandtab shiftwidth=4 ts=4 tw=80: