CGI::A...spatch(3pm)
π CGI::Application::Plugin::ActionDispatch β the manual, but make it cute π
okay bestie, gather round, because this is CGI::Application::Plugin::ActionDispatch from the perl manual pages (the 3pm one, you know the vibe) and it's genuinely so good. Perl extension, attribute-based routing, Catalyst girlies will recognize the energy. let's go β¨
πΈ NAME πΈ
CGI::Application::Plugin::ActionDispatch β a Perl extension. that's it, that's the name, no notes π
π SYNOPSIS π
so in your WebApp.pm, you're gonna do this:
# In "WebApp.pm"...
package WebApp;
use base 'CGI::Application';
use CGI::Application::Plugin::ActionDispatch;
sub do_stuff : Path('do/stuff') { ... }
sub do_more_stuff : Regex('^/do/more/stuff\/?$') { ... }
sub do_something_else : Regex('do/something/else/(\w+)/(\d+)$') { ... }
like. look at that. attributes doing the routing. we love to see it π¦
β¨ DESCRIPTION β¨
okay so what this plugin does: CGI::Application::Plugin::ActionDispatch adds attribute based support for parsing the PATH_INFO of the incoming request. if you've ever touched Catalyst, this interface works very similar β so if you're a Catalyst girlie, hi, you already know π«Ά
and the best part? this plugin is plug and play. it shouldn't interrupt the default behavior of CGI::Application. no cap. it just slides in and does its thing π
π CAVEATS π
be aware though β this plugin will not likely work with other modules that use attributes. sharing is not always caring, bestie.
this module should work with mod_perl. however it has not been thoroughly tested as such. so like... proceed with caution, it's giving untested π
if you have used it with mod_perl, please e-mail the author with your experience! he wants to know, fr fr π§
π METHODS π
π action_args()
if you're using capturing parentheses in a Regex action, the captured values are accessible using this method. so like:
sub addElement : Regex('add/(\d+)/(\d+)') {
my $self = shift;
my($column, $row) = $self->action_args();
...
}
and the Path action? it will store everything after the matched path into the action args. obsessed.
# http://example.com/state/pa/philadelphia
sub find_state_and_city : Path('state/') {
my $self = shift;
my($state, $city) = $self->action_args();
# $state == pa, $city == philadelphia
...
}
like it literally hands you pa and philadelphia on a silver platter. main character energy π
π₯ ACTIONS π₯
β Regex
Regex action is used for regular expression matching against PATH_INFO. if capturing parentheses are used, the matched parameters are accesssible using the action_args() method. (yes, "accesssible" β the original says it like that, and honestly? iconic. keeping it π)
Regex('^blah/foo');
the Regex action either matches or it doesn't. there are no secrets to it. it's giving straightforward, no drama π
it is important to note that Regex action takes priority. it is assumed if a Path and Regex action both match, the Regex action will take priority β which may not always be the outcome of least suprise, for instance:
# http://example.com/music/the_clash
sub clash : Path('/music/the_clash') {} # This is an exact match, BUT.
sub the_class : Regex('/music/the_clash') {} # This takes priority.
Beware.
so yeah. your exactPathmatch can get sniped by aRegexmatch. Beware. π that's not a vibe check you want to fail.
πΈ Path
the Path action is basically a shortcut for a commonly used Regex action. lowkey the friendliest one here.
# http://example.com/products/movies/2
sub show_product : Path('products/') {
my $self = shift;
my($category, $id) = $self->action_args();
....
}
is basically the same thing as:
sub show_product : Regex('^/products/(\w+)/(\d+)') {
my $self = shift;
my($category, $id) = $self->action_args();
...
}
and for those that care (the nerds, the scholars, the real ones), the Path('products/') will be converted to the regular expression "^/products\/?(\/.*)$"; then split('/') is run on the captured value and stored in action_args(). periodt π
π¦ Runmode
this action will take the method name and run a match on that.
# http://example.com/foobar
sub foobar : Runmode {}
name of the method = the thing it matches. simple, clean, ate β¨
π Default
the default run mode if no match is found. essentially the equivalent of the start_mode() method. it's the "if all else fails, come here" runmode, the emotional support runmode π«Ά
sub default_mode : Default {}
π EXAMPLE π
in the CGI::Application module:
package WebApp;
use base 'CGI::Application';
use CGI::Application::Plugin::ActionDispatch;
use strict;
sub setup {
my $self = shift;
self->mode_param('test_rm');
$self->run_modes(
basic_runmode => 'basic_runmode'
);
}
# Regular runmodes should work.
sub basic_runmode {
my $self = shift
}
good news bestie: regular runmodes should work. no disruption, we respect the classics π
the product() runmode will match anything starting with "/products" in the PATH_INFO.
# http://example.com/myapp.cgi/products/this/is/optional/and/stored/in/action_args/
sub product : Path('products/') {
my $self = shift;
my($category, $product) = $self->action_args();
}
the music() runmode will match anything starting with "/products/music" in the PATH_INFO. the product() runmode also matches "/products/music". however, since this runmode matches closer, it takes priority over product(). the specificity slay π―
# http://example.com/myapp.cgi/products/music/product/
sub music : Path('products/music/') {
my $self = shift;
my $product = $self->action_args();
...
}
this beatles() runmode will match ONLY "/product/music/beatles" or "/product/music/beatles/". Regex takes priority over Path, so the previous runmodes which match this PATH_INFO are not run. like, they're seated, they're not getting called π
# http://example.com/myapp.cgi/products/music/beatles/
sub beatles : Regex('^/products/music/beatles\/?') {
my $self = shift;
...
}
π§ SEE ALSO π§
CGI::ApplicationCGI::Application::Dispatchhttp://github.com/jaywhy/cgi-application-plugin-actiondispatch
go read the source, bestie, iykyk π
π AUTHOR π
Jason Yates, <jaywhy@gmail.com>
π COPYRIGHT AND LICENSE π
Copyright (C) 2006-2008 by Jason Yates
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.
and that's the whole manual, bestie!! you ate. no crumbs. π β¨
this page came to you from perl v5.34.0, dated 2022-06-09, and it lives under CGI::A...spatch(3pm) (yes the title's truncated, the man page headers have no chill). now go write some attributes and slay ππ¦π
π¦ where this page comes from
- debian bookworm: libcgi-application-plugin-actiondispatch-perl 0.99-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcgi-application-plugin-actiondispatch-perl 0.99-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcgi-application-plugin-actiondispatch-perl 0.99-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcgi-application-plugin-actiondispatch-perl 0.99-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcgi-application-plugin-actiondispatch-perl 0.99-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms