Permalink
Browse files

dispatch dispatch()

  • Loading branch information...
1 parent b82c323 commit c490f27031e5dcbff1869d5b0605440fee16505a Ryo Anazawa committed Sep 25, 2012
Showing with 31 additions and 29 deletions.
  1. +31 −29 lib/CGI/Header/Dispatcher.pm
View
60 lib/CGI/Header/Dispatcher.pm
@@ -128,47 +128,49 @@ my %Set_Cookie = (
);
my %Handler = (
- -content_disposition => \%Content_Disposition,
- -content_type => \%Content_Type,
- -cookie => \%Set_Cookie,
- -date => \%Date,
- -expires => \%Expires,
- -p3p => \%P3P,
+ -cookie => \%Set_Cookie, -content_disposition => \%Content_Disposition,
+ -date => \%Date, -content_type => \%Content_Type,
+ -expires => \%Expires, -p3p => \%P3P,
+);
+
+my %Dispatcher = (
+ get => sub {
+ my ( $self, $field, $norm, $handler ) = @_;
+ $handler ? $handler->( $self->header ) : $self->header->{ $norm };
+ },
+ set => sub {
+ my ( $self, $field, $norm, $handler, $value ) = @_;
+ $handler->( $self->header, $value ) if $handler;
+ $self->header->{ $norm } = $value unless $handler;
+ return;
+ },
+ exists => sub {
+ my ( $self, $field, $norm, $handler ) = @_;
+ $handler ? $handler->( $self->header ) : $self->header->{ $norm };
+ },
+ delete => sub {
+ my ( $self, $field, $norm, $handler ) = @_;
+ my $value = defined wantarray && $self->get( $field );
+ $handler->( $self->header ) if $handler;
+ delete $self->header->{ $norm };
+ $value;
+ },
);
sub dispatch {
my $self = shift;
my $operator = shift;
my $field = shift;
my $norm = _normalize( $field );
- my $header = $self->header;
return if !$operator or !$norm;
- my $handler = exists $Handler{ $norm } && $Handler{ $norm }{ $operator };
-
- if ( $operator eq 'get' ) {
- return $handler ? $handler->( $header ) : $header->{ $norm };
- }
- elsif ( $operator eq 'set' ) {
- my $value = shift;
- $handler->( $header, $value ) if $handler;
- $header->{ $norm } = $value unless $handler;
- }
- elsif ( $operator eq 'exists' ) {
- return $handler ? $handler->( $header ) : $header->{ $norm };
- }
- elsif ( $operator eq 'delete' ) {
- my $value = defined wantarray && $self->get( $field );
- $handler->( $header ) if $handler;
- delete $header->{ $norm };
- return $value;
- }
- else {
- croak "Unknown operator '$operator' passed to dispatch()";
+ if ( my $dispatch = $Dispatcher{$operator} ) {
+ my $handler = exists $Handler{$norm} && $Handler{$norm}{$operator};
+ return $self->$dispatch( $field, $norm, $handler, @_ );
}
- return;
+ croak "Unknown operator '$operator' passed to dispatch()";
}
my %norm_of = (

0 comments on commit c490f27

Please sign in to comment.