CGI::Header - Adapter for CGI::header()
use CGI::Header; my $header = { -attachment => 'foo.gif', -charset => 'utf-7', -cookie => [ $cookie1, $cookie2 ], # CGI::Cookie objects -expires => '+3d', -nph => 1, -p3p => [qw/CAO DSP LAW CURa/], -target => 'ResultsWindow', -type => 'image/gif', }; my $h = CGI::Header->new( $header ); # update $header $h->set( Content-Length' => 3002 ); $h->delete( 'Content-Disposition' ); $h->clear; my @headers = $h->flatten; # => ( 'Content-length', '3002', 'Content-Type', 'text/plain' ) print $h->as_string; # Content-length: 3002 # Content-Type: text/plain $h->header; # same reference as $header
Utility class to manipulate a hash reference which CGI's header() function receives.
header()
Given a header hash reference, returns a CGI::Header object which holds a reference to the original given argument. The object updates the reference when called write methods like set(), delete() or clear(). It also has header() method that would return the same reference.
set()
delete()
clear()
my $header = { -type => 'text/plain' }; my $h = CGI::Header->new( $header ); $h->header; # same reference as $header
Get or set the value of the header field. The header field name ($field) is not case sensitive. You can use underscores as a replacement for dashes in header names.
$field
# field names are case-insensitive $header->get( 'Content-Length' ); $header->get( 'content_length' );
The $value argument may be a plain string or a reference to an array of CGI::Cookie objects for the Set-Cookie header.
$value
$header->set( 'Content-Length' => 3002 ); $header->set( 'Set-Cookie' => [$cookie1, $cookie2] );
Returns a Boolean value telling whether the specified field exists.
if ( $header->exists('ETag') ) { ... }
Deletes the specified field form CGI response headers. Returns the value of the deleted field.
$header->delete( 'Content-Disposition' ); my $value = $header->delete( 'Content-Disposition' ); # inline
Returns the list of field names present in the header.
my @fields = $header->field_names; # => ( 'Set-Cookie', 'Content-length', 'Content-Type' )
Apply a subroutine to each header field in turn. The callback routine is called with two parameters; the name of the field and a value. Any return values of the callback routine are ignored.
my @lines; $self->each(sub { my ( $field, $value ) = @_; push @lines, "$field: $value"; });
Returns pairs of fields and values.
my @headers = $header->flatten; # => ( 'Status', '304 Nod Modified', 'Content-Type', 'text/plain' )
This will remove all header fields.
Internally, this method is a shortcut for:
%{ $header->header } = ( -type => q{} );
Returns true if the header contains no key-value pairs.
$header->clear; if ( $header->is_empty ) { # true ... }
Returns a copy of this CGI::Header object. It's identical to:
my %copy = %{ $header->header }; my $clone = CGI::Header->new( \%copy );
Returns the header fields as a formatted MIME header. The optional $eol parameter specifies the line ending sequence to use. The default is \015\012.
$eol
\015\012
The following:
use CGI; print CGI::header( $header->header );
is identical to:
my $CRLF = $CGI::CRLF; print $header->as_string( $CRLF ) . $CRLF;
A shortcut for
$header->set( 'Content-Disposition' => qq{attachment; filename="$filename"} );
$header->set( 'P3P' => qq{policyref="/w3c/p3p.xml", CP="$tags"} );
The Expires header gives the date and time after which the entity should be considered stale. You can specify an absolute or relative expiration interval. The following forms are all valid for this field:
$header->expires( '+30s' ); # 30 seconds from now $header->expires( '+10m' ); # ten minutes from now $header->expires( '+1h' ); # one hour from now $header->expires( 'now' ); # immediately $header->expires( '+3M' ); # in three months $header->expires( '+10y' ); # in ten years time # at the indicated time & date $header->expires( 'Thu, 25 Apr 1999 00:40:33 GMT' );
CGI, Plack::Util
This module is beta state. API may change without notice.
Ryo Anazawa (anazawa@cpan.org)
This module is free software; you can redistibute it and/or modify it under the same terms as Perl itself. See perlartistic.
To install CGI::Header, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Header
CPAN shell
perl -MCPAN -e shell install CGI::Header
For more information on module installation, please visit the detailed CPAN module installation guide.