Skip to content
This repository
Browse code

dispatch dispatch()

  • Loading branch information...
commit c490f27031e5dcbff1869d5b0605440fee16505a 1 parent b82c323
authored

Showing 1 changed file with 31 additions and 29 deletions. Show diff stats Hide diff stats

  1. 60  lib/CGI/Header/Dispatcher.pm
60  lib/CGI/Header/Dispatcher.pm
@@ -128,12 +128,33 @@ my %Set_Cookie = (
128 128
 );
129 129
 
130 130
 my %Handler = (
131  
-    -content_disposition => \%Content_Disposition,
132  
-    -content_type        => \%Content_Type,
133  
-    -cookie              => \%Set_Cookie,
134  
-    -date                => \%Date,
135  
-    -expires             => \%Expires,
136  
-    -p3p                 => \%P3P,
  131
+    -cookie  => \%Set_Cookie, -content_disposition => \%Content_Disposition,
  132
+    -date    => \%Date,       -content_type        => \%Content_Type,
  133
+    -expires => \%Expires,    -p3p                 => \%P3P,
  134
+);
  135
+
  136
+my %Dispatcher = (
  137
+    get => sub {
  138
+        my ( $self, $field, $norm, $handler ) = @_;
  139
+        $handler ? $handler->( $self->header ) : $self->header->{ $norm };
  140
+    },
  141
+    set => sub {
  142
+        my ( $self, $field, $norm, $handler, $value ) = @_;
  143
+        $handler->( $self->header, $value ) if $handler;
  144
+        $self->header->{ $norm } = $value unless $handler;
  145
+        return;
  146
+    },
  147
+    exists => sub {
  148
+        my ( $self, $field, $norm, $handler ) = @_;
  149
+        $handler ? $handler->( $self->header ) : $self->header->{ $norm };
  150
+    },
  151
+    delete => sub {
  152
+        my ( $self, $field, $norm, $handler ) = @_;
  153
+        my $value = defined wantarray && $self->get( $field );
  154
+        $handler->( $self->header ) if $handler;
  155
+        delete $self->header->{ $norm };
  156
+        $value;
  157
+    },
137 158
 );
138 159
 
139 160
 sub dispatch {
@@ -141,34 +162,15 @@ sub dispatch {
141 162
     my $operator = shift;
142 163
     my $field    = shift;
143 164
     my $norm     = _normalize( $field );
144  
-    my $header   = $self->header;
145 165
 
146 166
     return if !$operator or !$norm;
147 167
 
148  
-    my $handler = exists $Handler{ $norm } && $Handler{ $norm }{ $operator };
149  
-
150  
-    if ( $operator eq 'get' ) {
151  
-        return $handler ? $handler->( $header ) : $header->{ $norm };
152  
-    }
153  
-    elsif ( $operator eq 'set' ) {
154  
-        my $value = shift;
155  
-        $handler->( $header, $value ) if $handler;
156  
-        $header->{ $norm } = $value unless $handler;
157  
-    }
158  
-    elsif ( $operator eq 'exists' ) {
159  
-        return $handler ? $handler->( $header ) : $header->{ $norm };
160  
-    }
161  
-    elsif ( $operator eq 'delete' ) {
162  
-        my $value = defined wantarray && $self->get( $field );
163  
-        $handler->( $header ) if $handler;
164  
-        delete $header->{ $norm };
165  
-        return $value;
166  
-    }
167  
-    else {
168  
-        croak "Unknown operator '$operator' passed to dispatch()";
  168
+    if ( my $dispatch = $Dispatcher{$operator} ) {
  169
+        my $handler = exists $Handler{$norm} && $Handler{$norm}{$operator};
  170
+        return $self->$dispatch( $field, $norm, $handler, @_ );
169 171
     }
170 172
 
171  
-    return;
  173
+    croak "Unknown operator '$operator' passed to dispatch()";
172 174
 }
173 175
 
174 176
 my %norm_of = (

0 notes on commit c490f27

Please sign in to comment.
Something went wrong with that request. Please try again.