フレームワーク (3) - $obj->query() と $obj->param()

フレームワーク (3) - $obj->query() と $obj->param() #

 package CGI::Minimal::App;
 
 use strict;
 use warnings;
 
 use UNIVERSAL::require;
 
 sub query {
     my $self = shift;
     my $query = shift;
 
     # If query object is not exist, set a new query object.
     unless ( exists $self->{__QUERY_OBJ} ) {
         if ( defined $query ) {
             $self->{__QUERY_OBJ} = $query;
         }
         else {
             my $module = 'CGI::Minimal';
             unless ( exists $INC{$module} ) {
                 $module->require or die $!;
             }
             $self->{__QUERY_OBJ} = $module->new();
         }
     }
     return $self->{__QUERY_OBJ};
 }
 
 sub param : lvalue {
     my $self = shift;
     my @data = @_;
 
     # First use? Create new __PARAMS!
     $self->{__PARAMS} = {} unless exists $self->{__PARAMS};
 
     if ( ref($data[0]) eq 'HASH' ) {
         # param($hash_ref)
         %{$self->{__PARAMS}} = (%{$self->{__PARAMS}}, %{$data[0]});
     }
     elsif ( scalar(@data) % 2 == 0 ) {
         # param(@array)
         %{$self->{__PARAMS}} = (%{$self->{__PARAMS}}, @data);
     }
     scalar(@data) == 1 ? $self->{__PARAMS}->{$data[0]} : $self->{__PARAMS};
 }

C::A::P::Stash 的に param を使いたかった & lvalue 使ってみたかった

 $obj->param->{'hoge'} = 'fuga'; # OK
 $obj->param('hoge','fuga');     # OK
 $obj->param('hoge') = 'fuga'    # OK
 $obj->param({hoge => 'fuga'});  # OK
 $obj->param(hoge => 'fuga');    # OK
 $obj->param = {hoge => 'fuga'}  # OK
 
 print $obj->param->{'hoge'};    # fuga
 print $obj->param('hoge');      # fuga

See Also

Copyright © 髭。/ Hugo + hugo-book