Module-Generic
view release on metacpan or search on metacpan
lib/Module/Generic.pm view on Meta::CPAN
$ref->{ 'Crypt::Misc' } =
[
\&{"Crypt::Misc\::encode_b64"},
\&{"Crypt::Misc\::decode_b64"},
];
}
}
my $ref = ${"${class}\::HAS_B64"};
return( '' ) if( !scalar( keys( %$ref ) ) );
my $prefs = [];
if( $val eq 'Crypt::Misc' || $val eq 'CryptX' )
{
push( @$prefs, qw( Crypt::Misc MIME::Base64 ) );
}
# for any other value, including 'MIME::Base64'
else
{
push( @$prefs, qw( MIME::Base64 Crypt::Misc ) );
}
foreach my $mod ( @$prefs )
{
if( exists( $ref->{ $mod } ) && $self->_load_class( $mod ) )
{
return( $ref->{ $mod } );
}
}
return( '' );
}
PERL
# NOTE: _has_symbol()
lib/Module/Generic/HeaderValue.pm view on Meta::CPAN
=item value_max integer
=back
=head2 new_from_header
Takes a header value such as C<foo; bar=2> and and an hash or hash reference of options and this will parse it and return a new L<Module::Generic::HeaderValue> object.
If L</decode>, it will decode the value found, if any. For example:
my $hv = Module::Generic::HeaderValue->new_from_header( "site_prefs=lang%3Den-GB" );
would become token C<site_prefs> with value C<lang=en-GB>
It will set the value as an array reference that can be retrieved with L</value> and as a string with L</value_as_string>
If the value is made of a token and a token value, such as in the example above, the array will be 2-elements long:
["site_prefs", "lang=en-GB"]
otherwise, such as in the example of C<text/html: encoding=utf-8>, the value will be a 1-element long array reference:
["text/html"]
Use L</value_as_string>, so you do not have to worry about this.
Each attribute token found such as C<encoding> in the example above, will be converted to lowercase before added in the C<params> hash reference that can be accessed with L</params>
You can control what acceptable attribute length and attribute's value length is by setting L</token_max> and L</value_max> respectively. If it is set to 0, then it will be understood as no length limit.
=head2 new_from_multi
Takes a header value that contains potentially multiple values separated by a proper comma and this returns an array object (L<Module::Generic::Array>) of L<Module::Generic::HeaderValue> objects.
my $all = Module::Generic::HeaderValue->new_from_multi(
q{site_prefs=lang%3Den-GB; Path=/; Expires=Monday, 01-Nov-2021 17:12:40 GMT; SameSite=Strict, csrf=9849724969dbcffd48c074b894c8fbda14610dc0ae62fac0f78b2aa091216e0b.1635825594; Path=/account; Secure}
);
Note that the comma in this string is found to be a separator only when it is followed by some token itself followed by C<=>, C<;>, C<,> or the end of string.
Possible options are:
=over 4
=item C<decode>
lib/Module/Generic/HeaderValue.pm view on Meta::CPAN
=head2 as_string
Returns the object as a string suitable to be added in an HTTP header.
If L</encode> is set and there is a token value, then this will be url escaped.
An attribute value set to C<undef> will result in the attribute alone:
my $hv = Module::Generic::HeaderValue->new(
"site_prefs=lang%3Den-GB",
decode => 1,
encode => 1,
params => { secure => undef }
);
would result in:
site_prefs=lang%3Den-GB; secure
Optionally takes an array reference of parameter names to output first, followed by an optional hash or hash reference of options.
Supported options are:
=over 4
=item C<quote>
Boolean. Defaults to false.
lib/Module/Generic/HeaderValue.pm view on Meta::CPAN
If set to true, all parameter values will be wrapped in double quotes, regardless of whether quoting would otherwise be required. This is useful for headers such as C<Content-Disposition> in C<multipart/form-data> contexts, where L<RFC 7578|https://d...
# Force quoting for all parameter values
my $str = $hv->as_string( [qw( name filename )], { quote => 1 } );
# produces: form-data; name="pause99_add_uri_httpupload"; filename="Module-Generic-v1.2.1.tar.gz"
=back
=head2 decode
Boolean. If set to true, L</new_from_header> will uri-unescape the token value, if any. For example a header value of C<site_prefs=lang%3Den-GB> is made of a token C<site_prefs> and a token value C<lang%3Den-GB>, which once decoded will become C<lang...
=head2 encode
Boolean. If set to true, then L</as_string> will encode the token value, if any. See above in L</decode>.
=head2 original
Cache value of the object stringified. It could also be set during object instantiation to provide the original header value.
my $hv = Module::Generic::HeaderValue->new( 'foo', original => 'foo; bar=2' ) ||
t/13.header_value.t view on Meta::CPAN
pass( $t->{test} );
}
};
}
};
subtest 'stringify' => sub
{
my $tests =
[
{ value => 'site_prefs=lang%3Den-GB', params => { path => '/', expires => 'Monday, 01-Nov-2021 17:12:40 GMT', domain => 'www.example.com', secure => undef }, expect => 'site_prefs=lang%3Den-GB; domain=www.example.com; expires="Monday, 01-Nov-2021...
{ value => 'site_prefs=lang%3Den-GB', params => { path => '/', expires => 'Monday, 01-Nov-2021 17:12:40 GMT', domain => 'www.example.com' }, expect => 'site_prefs=lang%3Den-GB; domain=www.example.com; expires="Monday, 01-Nov-2021 17:12:40 GMT"; p...
];
foreach my $t ( @$tests )
{
my $expect = delete( $t->{expect} );
$t->{debug} = $DEBUG;
my $hv = Module::Generic::HeaderValue->new( delete( $t->{value} ) => $t );
my $res = $hv->as_string;
if( !defined( $res ) )
{
( run in 1.211 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )