Perl ã® Mixin / Catalyst ã®ãã©ã°ã¤ã³ã®ä»çµã¿
ä½ã¨ã¿ã¤ã ãªã¼ãªè¨äºãã¾ã¤ãã¨ããããä»æ§ã®ç¶æ¿ãã¨ãå®è£ ã®ç¶æ¿ãã«ã¤ãã¦èªã£ã¦ããã®ã§åæã«ã¾ã¨ãã¦ã¿ãã
ã¾ã¤ãã¨ãããITProã«æ¸ããè¨äºããã£ããã«ãªã£ãã®ããæè¿ Mixin ã«é¢ãã話é¡ãããç®ã«ãããæ°ããã¾ãã
Perl ã¯å¤éç¶æ¿ããµãã¼ãããä¸æ¹ã§ãExporter ã使ã£ã Mixin ãå¯è½ã§ãããã®è¾ºã¯miyagawa ããã®ã¡ã¼ã«ãã¬ã¸ã³ã®ããã¯ãã³ãã¼ã«è©³ããæ¸ãã¦ããã¾ãã
- Object-Oriented Programming with Perl Vol.29 - Exporter ã«ãã Mixin
- Object-Oriented Programming with Perl Vol.30 - Exporter::Lite
Exporter ã§é¢æ°ã export ãã¤ã¤ãããã import ããå´ã§ãé¢æ°ã§ã¯ãªãã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¨ãã¦ãããå©ç¨ããã¨ããããªãã¯ã使ããã¨ã§ Mixin ãã§ãããã¨ãããã®ã§ãããã ãä¸ã§ã触ãããã¦ããããã« $self ã®æ±ãã§æ³¨æã«ãªãã±ã¼ã¹ãããã¾ãã
ã¨ããã§ãPerl on Rails ãªãã¬ã¼ã ã¯ã¼ã¯ã® Catalyst ã®ãã©ã°ã¤ã³ã®ä»çµã¿ãè¦ã¦ãã¨èå³æ·±ãã³ã¼ããåºã¦ãã¾ãã
ä¾ãã° Catalyst ã« Data::FormValidator ãçµã¿è¾¼ãããã® Catalyst::Plugin::FormValidator ã®å®è£ ã¯
package Catalyst::Plugin::FormValidator; use strict; use NEXT; use Data::FormValidator; our $VERSION = '0.02'; sub form { my $c = shift; if ( $_[0] ) { my $form = $_[1] ? {@_} : $_[0]; $c->{form} = Data::FormValidator->check( $c->request->parameters, $form ); } return $c->{form}; } 1;
ã¨ãããªæããç¹ã«ä½ããç¶æ¿ãã¦ããããã§ã¯ããã¾ããã(NEXTããªãã¦ããã¡ã½ããã® redispatch ãå®ç¾ããã¢ã¸ã¥ã¼ã«?ãåºã¦ãã¦ã¾ããããã®è¾ºããã©ã°ã¤ã³ã¢ã¼ããã¯ãã£ã«ã¯é¢ä¿ãã¦ã¾ããã ä»åã®è©±ã¨ã¯é¢ä¿ããã¾ããã)
ã§ãããCatalyst ã使ãã¨ãã«
use Catalyst qw (/FormValidator/)
ã¨ãã¦ããã¨ãController ã®ä¸ã¨ãã§
sub default : Private { my ( $self, $c ) = @_; my $result = $c->form( required => [qw(name title)], ); if ($result->has_invalid or $result->has_missing) { ... }
ã¨ãã£ãæãã§ãã³ã³ããã¹ããªãã¸ã§ã¯ã ($c) ã§ãCatalyst::Plugin::FormValidator ã§å®ç¾©ãã¦ãã form ã¡ã½ããã使ããããã«ãªãã¾ããã³ã³ããã¹ããªãã¸ã§ã¯ãã¨ããã®ã¯ Catalyst ã®ã³ã¢ã¿ãããªããã§ãããã¤ããã¯ã¨ãªãåå¾ããããã³ãã¬ã¼ããã»ãããããã¨ãããã¨ãã§ãã¾ãã
æ示çãªç¶æ¿ã使ã£ã¦ããªãã¯ã©ã¹ãæ¸ãã ãã§ãã³ã¢ãæ¡å¼µãããããã«ãªã£ã¦ãããã¨ããã¨ããããã¤ã³ãã§ãããã®æ¯ãèã㯠Mixin ã«è¿ããã®ãããã¾ãã
ãã®ã«ã©ã¯ãªã¯ Catalyst::Setup ã® setup_plugins ã¡ã½ããã«ãã£ã¦ã以ä¸ã®ãããªã³ã¼ãã«ãªã£ã¦ã¾ãã($plugins ã«ã¯ use Catalyst ã§æå®ãããã©ã°ã¤ã³åã渡ãã¾ãã)
sub setup_plugins { my ( $class, $plugins ) = @_; for my $plugin ( @$plugins ) { $plugin = "Catalyst::Plugin::$plugin"; $plugin->require; if ( $@ ) { Catalyst::Exception->throw( message => qq/Couldn't load plugin "$plugin", "$@"/ ); } { no strict 'refs'; push @{"$class\::ISA"}, $plugin; } } }
Catalyst ã®ã³ã¢ã¯ãã® Catalyst::Setup ãç¶æ¿ãã¦ãã(ãã®è¾ºã®ç¶æ¿é¢ä¿ã¯çµæ§ããªãã¯ãå¤ç¨ããã¦ããã®ã§èª¬æããã®ãã¡ãã£ã¨é£ãã)ããã® Catalyst::Setup ã®ç¶æ¿ããªã¼ã«å¯¾ãã¦
no strict 'refs'; push @{"$class\::ISA"}, $plugin;
ã¨ãã©ã°ã¤ã³ã®ã¯ã©ã¹åãå·®ãè¾¼ããã¨ãããã¨ããã¦ã¾ããããã«ãããPlugin ãã£ã¬ã¯ããªã«ãã©ã°ã¤ã³ã®ã¯ã©ã¹ã追å ããã ãã§ãç¹ã«ãã®ãã©ã°ã¤ã³ã¯ã©ã¹ãä½ããç¶æ¿ããããã³ã³ã¹ãã©ã¯ã¿ãå®ç¾©ãããããªãã¦ããã³ã¢ãæ¡å¼µãããã¨ãã§ããããã«ãªã£ã¦ã¾ãã
å®éã«ã¯ç¶æ¿ããªã¼ã®éä¸ (@Catalyst::Setup::ISA) ã« push ããããã¦ãã®ã§å¤éç¶æ¿ãã¨ãããã¨ã«ãªãã®ã§ãããããã©ã°ã¤ã³ãéçºããå´ããè¦ã㨠Mixin ã«è¿ãæ±ãã«ãªã£ã¦ãã¾ããã¾ããã©ã°ã¤ã³ã®ã¯ã©ã¹ã¯å®éã«ç¶æ¿ããªã¼ã®ä¸ã«å ¥ãããã$self åé¡ã¯ããã¾ããããªããªãé¢ç½ãä»çµã¿ã§ãã
åçãªè¨èªã®å¼·ã¿ãçãããå®è£ ãã¨ããæãã§ããããã®ä»çµã¿ãé¢ç½ãã£ãã®ã§ãã¯ã¦ãªãã¬ã¼ã ã¯ã¼ã¯ã«ãåæ§ã®ãã©ã°ã¤ã³ãã¸ãã¯ãçµã¿è¾¼ãã§ã¿ã¾ããã