CGI::A...spatch(3pm)

πŸ“¦
version 0.99 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 exact Path match can get sniped by a Regex match. 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 🧚

go read the source, bestie, iykyk πŸ‘‘


πŸ’– AUTHOR πŸ’–

Jason Yates, <jaywhy@gmail.com>


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