CGI-Easy
view release on metacpan or search on metacpan
lib/CGI/Easy.pm view on Meta::CPAN
some other useful modules available separately: L<CGI::Easy::URLconf>,
L<CGI::Easy::SendFile>.
CGI::Easy designed to help you do what you want with CGI/HTTP without
forcing you to learn I<one more> huge and complex API specific to some
module, or limiting you to do your tasks I<only in way> provided by this
module. With CGI::Easy you got all you need in B<simple hashes>, and
you're free to B<do anything you like> with this data, because it's
B<your data>.
CGI::Easy consist of three main parts:
=over
=item CGI::Easy::Request object
This object actually is simple hash populated with all data related to
current CGI request - GET/POST parameters, cookies, url path, ⦠When you
create this object with new(), current request will be parsed (from C< %ENV >
and C< STDIN >), all useful things will be stored in that object/hash, and
now you're free to do anything you want with this object/hash - modify it
contents in any way, etc. You don't need special methods to access trivial
data like some GET parameter or cookie anymore.
Here is list of keys in that hash prepared for you:
# -- URL info
scheme 'http' OR 'https'
host 'example.com'
port 80
path '/' OR '/index.php' OR '/articles/2008/'
# -- CGI parameters
GET { name => 'powerman', 'color[]' => ['red','green'], ⦠}
POST { name => 'powerman', avatar => 'â¦binary image dataâ¦', ⦠}
filename { name => undef, avatar => 'C:\\Documents\\avatar.png', ⦠}
mimetype { name => undef, avatar => 'image/png', ⦠}
cookie { somevar => 'someval', ⦠}
# -- USER details
REMOTE_ADDR 192.168.2.1
REMOTE_PORT 12345
AUTH_TYPE Basic
REMOTE_USER 'powerman'
REMOTE_PASS 'secret'
# -- original request data
ENV { REQUEST_METHOD => 'POST', ⦠}
STDIN 'name=powerman&color[]=red&color[]=green'
# -- request parsing status
error '' OR 'POST body too large' etc.
=item CGI::Easy::Headers object
This object is also very simple hash - keys are HTTP header names and
values are HTTP header values. When you call new() this hash populated
with few headers (notably C<< 'Status'=>'200 OK' >> and
C<< 'Content-Type'=>'text/html; charset=utf-8' >>), but you're free to
change these keys/headers and add your own headers. When you ready to
output all headers from this object/hash you should call compose() method,
and it will return string with all HTTP headers suitable for sending to
browser.
There one exception: value for key 'Set-Cookie' is ARRAYREF with HASHREF,
where each HASHREF keep cookie details:
$h->{'Set-Cookie'} = [
{ name=>'mycookie1', value=>'myvalue1' },
{ name=>'x', value=>5,
domain=>'.example.com', expires=>time+86400 }
];
To make it ease for you to work with this key there helper add_cookie()
method available, but you're free to modify this key manually if you like.
There also some helper methods in this object (like redirect()), but they
all just modify some keys/headers in this hash.
=item CGI::Easy::Session object
This object make working with cookies even more ease than already provided
by CGI::Easy::Request and CGI::Easy::Headers way:
my $somevalue = $r->{cookie}{somename};
$h->add_cookie({ name => 'somename', value => $somename });
If you will use CGI::Easy::Session, then it will read/write values for
three cookies: C<sid>, C<perm> and C<temp>. Cookie C<sid> will contain
automatically generated ID unique to this visitor, cookies C<perm> and
C<temp> will contain simple perl hashes (automatically serialized to
strings for storing in cookies) with different lifetime: C<perm> will
expire in 1 year, C<temp> will expire when browser closes.
CGI::Easy::Session object will provide you with three keys:
id undef OR 'â¦unique stringâ¦'
perm { x=>5, somename=>'somevalue', ⦠}
temp { y=>7, ⦠}
Field C<id> will contain undef() in case user has no cookie support.
To serialize hashes in fields C<perm> and C<temp> to cookies you'll have
to call save() method before C<< $h->compose() >>. Example:
if (!defined $sess->{id}) {
warn "user has no cookie support";
}
$sess->{perm}{x} = 5;
$sess->{perm}{somename} = 'somevalue';
$sess->{temp}{y}++;
$sess->save();
print $h->compose();
=back
You don't have to use all these three parts - for example, you can use
only CGI::Easy::Request and output HTTP headers manually, or use only
CGI::Easy::Headers and parse CGI parameters using standard L<CGI> module,
etc.
=head2 Unicode
These modules by default support Unicode with UTF8 encoding. If you need
another encoding or wanna disable Unicode look at C< raw > option for
CGI::Easy::Request->new() and modify default C< 'Content-Type' > header
provided by CGI::Easy::Headers->new().
=head1 EXAMPLES
=head2 CGI with Session
use CGI::Easy::Request;
use CGI::Easy::Headers;
use CGI::Easy::Session;
my $r = CGI::Easy::Request->new();
my $h = CGI::Easy::Headers->new();
my $sess = CGI::Easy::Session->new($r, $h);
$sess->{perm}{create_time} ||= time;
$sess->{temp}{counter} ||= 0;
$sess->{temp}{counter}++;
$sess->save();
print $h->compose();
if ($sess->{id}) {
( run in 0.738 second using v1.01-cache-2.11-cpan-39bf76dae61 )