view release on metacpan or search on metacpan
* Change message() to snippet()
NOTE - already changed Badger::Base XXX_msg() method to explicitly
package-scope message() to Badger::Base so that problem is mitigated.
A subclass can now define a message() method and it will still Just Work[tm]
Badger::Codec
--------------
* encoded()/decoded() methods (e.g. for utf8)
* update docs to clarify the fact that utf8, etc., are available as named
codecs via Encode/Encoding modules.
Badger::Config
--------------
* Just a basic implementation at moment. Merge in TT3 config, AppConfig
and other stuff.
lib/Badger.pm view on Meta::CPAN
Returns a L<Badger::Hub> object.
=head2 codec()
Delegates to the L<Badger::Hub> L<codec()|Badger::Hub/codec()> method to
return a L<Badger::Codec> object.
my $base64 = Badger->codec('base64');
my $encoded = $base64->encode($uncoded);
my $decoded = $base64->decode($encoded);
=head2 config()
Delegates to the L<Badger::Hub> L<codec()|Badger::Hub/codec()> method to
return a L<Badger::Config> object. This is still experimental.
=head1 TODO
Other methods like L<codec()> to access different C<Badger> modules.
These should be generated dynamically on demand.
lib/Badger/Class.pm view on Meta::CPAN
L<configure()|Badger::Class::Config/configure()> method uses.
=head2 codec
This can be used to import a single codec from L<Badger::Codecs>.
use Badger::Class
codec => 'base64';
my $encoded = encode('Some text');
my $decoded = decode($encoded);
See the L<codec()> method and L<Badger::Codecs> for further details.
=head2 codecs
This can be used to import multiple codecs from L<Badger::Codecs>.
use Badger::Class
codecs => 'base64 storable';
my $encoded = encode_base64( encode_storable( $some_data ) );
my $decoded = decode_storable( decode_base64( $encoded ) );
Codecs can be composed as a pipeline of other codecs. In the following
example, we define a C<session> codec which encodes data by first passing it
through the C<storable> codec (which uses the L<Storable> C<freeze()>
subroutine) and then onto the C<base64> codec (which uses the L<MIME::Base64>
C<encode_base64> subroutine).
use Badger::Class
codecs => {
session => 'base64+storable',
};
my $encoded = encode_session( $some_data );
my $decoded = decode_session( $encoded );
In case you were wondering about the significance of this particular codec
combination, the C<Storable> module can generate NULL characters in the
output stream which will make some databases (e.g. Postgres) choke. Adding
a second level of Base 64 encoding solves the problem.
See the L<codecs()> method and L<Badger::Codecs> for further details.
=head2 methods
lib/Badger/Codec.pm view on Meta::CPAN
sub encode {
my ($self, $data) = @_;
# do something
return $encoded_data;
}
sub decode {
my ($self, $encoded_data) = @_;
# do something
return $decoded_data;
}
# using the subclass:
use My::Codec;
my $codec = My::Codec->new();
my $encoded = $codec->encode($some_data);
my $decoded = $codec->decode($encoded);
=head1 INTRODUCTION
This documentation describes the inner working of codec modules. You should
only need to consult this documentation if you're writing a codec subclass.
For a general introduction to codecs and examples of use, please see
L<Badger::Codecs>.
=head1 DESCRIPTION
lib/Badger/Codec/Base64.pm view on Meta::CPAN
=head1 NAME
Badger::Codec::Base64 - encode/decode data using MIME::Base64
=head1 SYNOPSIS
use Badger::Codec::Base64;
my $codec = Badger::Codec::Base64->new();
my $encoded = $codec->encode("Hello World");
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module implements a subclass of L<Badger::Codec> which uses the
C<encode_base64> and C<decode_base64> subroutines provided by the
L<MIME::Base64> module to encode and decode data.
It a very thin wrapper around the C<MIME::Base64> module and offers no
functional advantage over it. It exist only to provide a consistent
API with other L<Badger::Codec> modules.
lib/Badger/Codec/Base64.pm view on Meta::CPAN
=head2 encode($data)
Encodes the data referenced by the first argument using C<encode_base64()>.
$encoded = Badger::Codec::Base64->encode($data);
=head2 decode($data)
Decodes the encoded data passed as the first argument using C<decode_base64()>.
$decoded = Badger::Codec::Base64->decode($encoded);
=head2 encoder()
This method returns a reference to the real subroutine that's doing
all the encoding work, i.e. the C<encode_base64()> method in L<MIME::Base64>.
=head2 decoder()
This method returns a reference to the real subroutine that's doing
all the decoding work, i.e. the C<decode_base64()> method in L<MIME::Base64>.
lib/Badger/Codec/Chain.pm view on Meta::CPAN
Encodes the data referenced by the first argument using all the
codecs in the chain.
$encoded = $codec->encode($data);
=head2 decode($html)
Decodes the encoded data passed as the first argument using all
the codecs in the chain B<in reverse order>.
$decoded = $codec->decode($encoded);
=head2 encoder()
Returns a reference to a subroutine which performs the encoding operation.
=head2 decoder()
Returns a reference to a subroutine which performs the decoding operation.
=head1 INTERNAL METHODS
lib/Badger/Codec/Encode.pm view on Meta::CPAN
=head1 NAME
Badger::Codec::Encode - codec wrapper around Encode
=head1 SYNOPSIS
use Badger::Codec::Encode;
my $codec = Badger::Codec::Encode->new();
my $encoded = $codec->encode( utf8 => "...some utf8 data..." );
my $decoded = $codec->decode( utf8 => $encoded );
=head1 DESCRIPTION
This module is a subclass of L<Badger::Codec> implementing a very thin wrapper
around the L<Encode> module. It exists only to provide a consistent API with
other L<Badger::Codec> modules and to facilitate codec chaining.
You would normally use a codec via the L<Badger::Codecs> module.
use Badger::Codecs
codec => 'encode';
my $encoding = 'UTF-8';
my $uncoded = "...some UTF-8 data...";
my $encoded = encode($encoding, $uncoded);
my $decoded = decode($encoding, $encoded)
The above example is identical to using the L<Encode> module directly:
use Encode; # also exports encode()/decode()
In addition, a L<Badger::Codec::Encode> object will be available via
the C<codec()> subroutine.
my $encoded = codec->encode($encoding, $uncoded);
my $decoded = codec->decode($encoding, $encoded)
=head1 METHODS
=head2 encode($encoding, $data)
Method for encoding data which forwards all arguments to the L<Encode>
C<encode()> method. The first argument is the encoding,
the second is the data to encode.
$encoded = Badger::Codec::Encode->encode( utf8 => $data );
=head2 decode($encoding, $data)
Method for decoding data which forwards all arguments to the L<Encode>
C<decode()> method. The first argument is the encoding, the second is the data
to decode.
$decoded = Badger::Codec::Encode->decode( utf8 => $encoded );
=head2 encoder()
This method returns a reference to the real subroutine that's doing all the
encoding work, i.e. the C<encode()> function in L<Encode>.
=head2 decoder()
This method returns a reference to the real subroutine that's doing all the
encoding work, i.e. the C<decode()> method in L<Encode>.
lib/Badger/Codec/Encoding.pm view on Meta::CPAN
=head1 SYNOPSIS
package My::Encoding::utf8;
use base 'Badger::Codec::Encoding';
use constant encoding => 'utf8';
package main;
my $codec = My::Encoding::utf8->new;
my $encoded = $codec->encode("...some utf8 data...");
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module is a subclass of L<Badger::Codec> which itself acts as a base
class for various specific encoding modules.
=head1 METHODS
=head2 encoding()
lib/Badger/Codec/Encoding.pm view on Meta::CPAN
subroutine to do all the hard work.
$encoded = $codec->encode($uncoded);
=head2 decode($data)
Method for decoding data. It uses the L<encoding()> method to determine
the encoding type and then calls the L<Encode> L<decode()|Encode/decode()>
subroutine to do all the hard work.
$decoded = $codec->decode($encoded);
=head2 encoder()
This method returns a subroutine reference which can be called to encode
data.
my $encoder = $codec->encode;
$encoded = $encoder->($data);
=head2 decoder()
This method returns a suboroutine reference which can be called to decode
data.
my $decoder = $codec->decode;
$decoded = $decoder->($data);
=head1 AUTHOR
Andy Wardley L<http://wardley.org/>
=head1 COPYRIGHT
Copyright (C) 2005-2009 Andy Wardley. All rights reserved.
=head1 SEE ALSO
lib/Badger/Codec/JSON.pm view on Meta::CPAN
=head1 NAME
Badger::Codec::JSON - encode/decode data using JSON
=head1 SYNOPSIS
use Badger::Codec::JSON;
my $codec = Badger::Codec::JSON->new();
my $encoded = $codec->encode({ msg => "Hello World" });
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module implements a subclass of L<Badger::Codec> which uses the
L<JSON::XS> or L<JSON> module (whichever you have installed) to encode and
decode data to and from JSON. It is little more than an adapter module
to fit L<JSON> into the L<Badger::Codec> mould.
=head1 METHODS
lib/Badger/Codec/JSON.pm view on Meta::CPAN
Encodes the Perl data in C<$data> to a JSON string. This method is a wrapper
around the internal the L<encode_json()> subroutine.
$encoded = Badger::Codec::JSON->encode($data);
=head2 decode($json)
Decodes the encoded JSON string in C<$json> back into a Perl data structure.
This method is a wrapper around the internal the L<decode_json()> subroutine.
$decoded = Badger::Codec::JSON->decode($encoded);
=head2 encoder()
This method returns a reference to the real subroutine that's doing
all the encoding work, i.e. the internal C<encode_json()> subroutine.
=head2 decoder()
This method returns a reference to the real subroutine that's doing
all the decoding work, i.e. the C<decode_json()> subroutine in L<JSON>.
lib/Badger/Codec/Storable.pm view on Meta::CPAN
=head2 encode($data)
Encodes the data referenced by the first argument using C<freeze()>.
$encoded = Badger::Codec::Storable->encode($data);
=head2 decode($html)
Decodes the encoded data passed as the first argument using C<thaw()>.
$decoded = Badger::Codec::Storable->decode($encoded);
=head2 encoder()
This method returns a reference to the real subroutine that's doing
all the encoding work, i.e. the C<freeze()> method in L<Storable>.
=head2 decoder()
This method returns a reference to the real subroutine that's doing
all the decoding work, i.e. the C<thaw()> method in L<Storable>.
lib/Badger/Codec/TT.pm view on Meta::CPAN
=head1 NAME
Badger::Codec::TT - encode/decode data using TT data syntax
=head1 SYNOPSIS
use Badger::Codec::TT;
my $codec = Badger::Codec::TT->new();
my $encoded = $codec->encode({ msg => "Hello World" });
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module implements a subclass of L<Badger::Codec> which encodes and
decodes data to and from an extended form of the data definition syntax used
in the Template Toolkit. It mainly exists for testing purposes (so that we
don't require people to install YAML or JSON just to run some of the Badger
tests) and to support some legacy systems that use data encoded in this way
(mostly dating back to the days before YAML and JSON were around). If you're
starting out afresh then you're probably better off using YAML or JSON unless
lib/Badger/Codec/TT.pm view on Meta::CPAN
=head2 encode($data)
Encodes the Perl data in C<$data> to a TT string.
$encoded = Badger::Codec::TT->encode($data);
=head2 decode($tt)
Decodes the encoded TT string in C<$tt> back into a Perl data structure.
$decoded = Badger::Codec::TT->decode($encoded);
=head2 encoder()
This method returns a reference to an encoding subroutine.
my $sub = Badger::Codec::TT->encoder;
$encoded = $sub->($data);
=head2 decoder()
This method returns a reference to a decoding subroutine.
my $sub = Badger::Codec::TT->decoder;
$decoded = $sub->($encoded);
=head1 INTERNAL SUBROUTINES
=head2 _encode($data)
This internal subroutine performs the recursive encoding of the data.
=head2 _decode($tt)
This internal subroutine performs the recursive decoding of the data.
lib/Badger/Codec/Timestamp.pm view on Meta::CPAN
Badger::Codec::Timestamp - encode/decode a timestamp via Badger::Timestamp
=head1 SYNOPSIS
use Badger::Codec::Timestamp;
use Badger::Timestamp 'Now';
my $codec = Badger::Codec::Timestamp->new();
my $encoded = $codec->encode(Now);
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module implements a subclass of L<Badger::Codec> for encoded and decoding
timestamps using the Badger::Timestamp module. It is trivially simple,
existing only to provide a consistent API with other L<Badger::Codec> modules.
It is typically used as a codec for reading and writing timestamps to and from
a file via the L<Badger::Filesystem> modules.
use Badger::Filesystem 'File';
lib/Badger/Codec/URI.pm view on Meta::CPAN
=head1 NAME
Badger::Codec::URI - URI encode/decode
=head1 SYNOPSIS
use Badger::Codec::URI;
my $codec = Badger::Codec::URI->new();
my $encoded = $codec->encode("Hello World!");
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module implements a subclass of L<Badger::Codec> for
URI encoding and decoding.
=head1 FUNCTIONS
=head2 encode_uri($data)
lib/Badger/Codec/URI.pm view on Meta::CPAN
This method URI-encodes the data referenced by the first argument.
It delegates to the L<encode_uri()> function.
$encoded = Badger::Codec::URI->encode($data);
=head2 decode($data)
This method decodes the encoded data passed as the first argument.
It delegates to the L<decode_uri()> function.
$decoded = Badger::Codec::URI->decode($encoded);
=head2 encoder()
This method returns a reference to the L<encode_uri()> function.
=head2 decoder()
This method returns a reference to the L<decode_uri()> function.
=head1 AUTHOR
lib/Badger/Codec/URL.pm view on Meta::CPAN
=head1 NAME
Badger::Codec::URL - URL encode/decode
=head1 SYNOPSIS
use Badger::Codec::URL;
my $codec = Badger::Codec::URL->new();
my $encoded = $codec->encode("Hello World!");
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module implements a subclass of L<Badger::Codec> for
URL encoding and decoding. Note the difference between URI and
URL. URI encoding is strict and encodes characters like C<;>, C<?>
and C</>. The URL codec is more lax and does not encode these characters.
The URI codec should be used for encoding URL parameters. The URL codec
can be used to encode complete URLs.
lib/Badger/Codec/URL.pm view on Meta::CPAN
This method URL-encodes the data referenced by the first argument.
It delegates to the L<encode_url()> function.
$encoded = Badger::Codec::URL->encode($data);
=head2 decode($data)
This method decodes the encoded data passed as the first argument.
It delegates to the L<decode_url()> function.
$decoded = Badger::Codec::URL->decode($encoded);
=head2 encoder()
This method returns a reference to the L<encode_url()> function.
=head2 decoder()
This method returns a reference to the L<decode_url()> function.
=head1 AUTHOR
lib/Badger/Codec/Unicode.pm view on Meta::CPAN
=head1 NAME
Badger::Codec::Unicode - encode/decode Unicode
=head1 SYNOPSIS
use Badger::Codec::Unicode;
my $codec = Badger::Codec::Unicode->new();
my $uncoded = "...some Unicode data...";
my $encoded = $codec->encode($uncoded);
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module is a subclass of L<Badger::Codec> implementing a very thin wrapper
around the L<Encode> module for encoding and decoding Unicode.
A C<Badger::Codec::Unicode> object provides the L<encode()> and L<decode()>
methods for encoding and decoding Unicode.
use Badger::Codec::Unicode;
my $codec = Badger::Codec::Unicode->new();
my $uncoded = "...some Unicode data...";
my $encoded = $codec->encode($uncoded);
my $decoded = $codec->decode($encoded);
You can also call L<encode()> and L<decode()> as class methods.
my $encoded = Badger::Code::Unicode->encode($uncoded);
my $decoded = Badger::Code::Unicode->decode($encoded);
You can also use a codec via the L<Badger::Codecs> module.
use Badger::Codecs
codec => 'unicode';
This exports the C<encode()> and C<decode()> subroutines.
my $uncoded = "...some Unicode data...";
my $encoded = encode($uncoded);
my $decoded = decode($encoded)
=head1 METHODS
=head2 encode($encoding, $data)
Method for encoding Unicode data. If two arguments are provided then
the first is the encoding and the second the data to encode.
$encoded = $codec->encode( utf8 => $data );
If one argument is provided then the encoding defaults to C<UTF-8>.
$utf8 = $codec->encode($data);
=head2 decode($encoding, $data)
Method for decoding Unicode data. If two arguments are provided then
the first is the encoding and the second the data to decode.
$decoded = $codec->decode( utf8 => $encoded );
If one argument is provided then the method will look for a Byte Order
Mark (BOM) to determine the encoding. If a BOM isn't present, or if the
BOM doesn't match a supported Unicode BOM (any of C<UTF-8>, C<UTF-32BE>
C<UTF-32LE>, C<UTF-16BE> or C<UTF-16LE>) then the data will not be
decoded as Unicode.
$decoded = $codec->decode($encoded); # use BOM to detect encoding
=head2 encoder()
This method returns a subroutine reference which can be called to encode
Unicode data. Internally it calls the L<encode()> method.
my $encoder = $codec->encode;
$encoded = $encoder->($data);
=head2 decoder()
This method returns a suboroutine reference which can be called to decode
Unicode data. Internally it calls the L<decode()> method.
my $decoder = $codec->decode;
$decoded = $decoder->($data);
=head1 AUTHOR
Andy Wardley L<http://wardley.org/>
=head1 COPYRIGHT
Copyright (C) 2005-2009 Andy Wardley. All rights reserved.
=head1 SEE ALSO
lib/Badger/Codec/YAML.pm view on Meta::CPAN
=head1 NAME
Badger::Codec::YAML - encode/decode data using YAML
=head1 SYNOPSIS
use Badger::Codec::YAML;
my $codec = Badger::Codec::YAML->new();
my $encoded = $codec->encode("Hello World");
my $decoded = $codec->decode($encoded);
=head1 DESCRIPTION
This module implements a subclass of L<Badger::Codec> which uses the
L<YAML> module to encode and decode data to and from YAML.
=head1 METHODS
=head2 encode($data)
Encodes C<$data> to YAML.
$encoded = Badger::Codec::YAML->encode($data);
=head2 decode($data)
Decodes C<$data> from YAML.
$decoded = Badger::Codec::YAML->decode($encoded);
=head2 encoder()
This method returns a reference to the real subroutine that's doing
all the encoding work, i.e. the C<Dump()> subroutine in L<YAML>.
=head2 decoder()
This method returns a reference to the real subroutine that's doing
all the decoding work, i.e. the C<Load()> subroutine in L<YAML>.
lib/Badger/Codecs.pm view on Meta::CPAN
=head1 NAME
Badger::Codecs - modules for encoding and decoding data
=head1 SYNOPSIS
# using class methods
use Badger::Codecs;
$encoded = Badger::Codecs->encode( base64 => $original );
$decoded = Badger::Codecs->decode( base64 => $encoded );
# creating a single codec object
$codec = Badger::Codecs->codec('base64');
$encoded = $codec->encode($original);
$decoded = $codec->decode($encoded);
# creating a codecs collection
$codecs = Badger::Codecs->new(
base => ['My::Codec', 'Badger::Codec'],
codecs => {
# most codec names are grokked automatigally from the
# base defined above - this hash is for any exceptions
wibble => 'Ferret::Codec::Wibble',
frusset => 'Stoat::Codec::Frusset',
}
);
# encode/decode via codecs collective
$encoded = $codecs->encode( wibble => $original );
$decoded = $codecs->decode( wibble => $encoded );
# or via a specific codec
$codec = $codecs->codec('wibble');
$encoded = $codec->encode($original);
$decoded = $codec->decode($encoded);
# importing a single codec
use Badger::Codecs
codec => 'url';
# codec() returns a Badger::Codec::URL object
$encoded = codec->encode($text);
$decoded = codec->decode($encoded);
# encode() and decode() are imported subroutines
$encoded = encode($text);
$decoded = decode($encoded);
# import multiple codecs
use Badger::Codecs
codecs => 'base64 storable';
# codec objects
base64->encode(...); base64->decode(...);
storable->encode(...); storable->decode(...);
# imported subroutines
lib/Badger/Codecs.pm view on Meta::CPAN
First you need to load the C<Badger::Codecs> module.
use Badger::Codecs;
It can be used in regular OO style by first creating a C<Badger::Codecs>
object and then calling methods on it.
my $codecs = Badger::Codecs->new();
my $codec = $codecs->codec('url');
my $encoded = $codec->encode($original);
my $decoded = $codec->decode($encoded);
You can also call class methods directly.
my $codec = Badger::Codecs->codec('url');
my $encoded = $codec->encode($original);
my $decoded = $codec->decode($encoded);
Or like this:
my $encoded = Badger::Codecs->encode(url => $original);
my $decoded = Badger::Codecs->decode(url => $encoded);
These examples are the equivalent of:
use Badger::Codec::URL;
my $codec = Badger::Codec::URL->new;
my $encoded = $codec->encode($original);
my $decoded = $codec->decode($encoded);
C<Badger::Codecs> will do its best to locate and load the correct codec module
for you. It defines a module base path (containing C<Badger::Codec> and
C<BadgerX::Codec> by default) to which the name of the requested codec is
appended in various forms.
It first tries the name exactly as specified. If no corresponding codec
module is found then it tries a capitalised version of the name, followed
by an upper case version of the name. So if you ask for a C<foo> codec,
then you'll get back a C<Badger::Codec::foo>, C<Badger::Codec::Foo>,
lib/Badger/Codecs.pm view on Meta::CPAN
=head2 Chained Codecs
Codecs can be chained together in sequence. Specify the names of the
individual codes separated by C<+> characters. Whitespace between the names
and C<+> is optional. The codec chain returned (L<Badger::Codec::Chain>)
behaves exactly like any other codec. The only difference being that it
is apply several codecs in sequence.
my $codec = Badger::Codecs->codec('storable+base64');
$encoded = $codec->encode($data); # encode storable then base64
$decoded = $codec->decode($encoded); # decode base64 then storable
Note that the decoding process for a chain happens in reverse order
to ensure that a round trip between L<encode()> and L<decode()> returns
the original unencoded data.
=head2 Import Hooks
The C<codec> and C<codecs> import hooks can be used to load and define
codec subroutines into another module.
lib/Badger/Codecs.pm view on Meta::CPAN
use Badger::Codecs
codec => 'base64';
The C<codec> import hook defines a C<codec()> subroutine which returns a
reference to a codec object. It also defined C<encode()> and C<decode()>
subroutines which are mapped to the codec.
# using the codec reference
$encoded = codec->encode($original);
$decoded = codec->decode($encoded);
# using the encode/decode subs
$encoded = encode($original);
$decoded = decode($encoded);
The C<codecs> import hook allows you to define several codecs at once. A
subroutine is generated to reference each codec, along with encoding and
decoding subroutines.
use Badger::Codecs
codecs => 'base64 storable';
# codec objects
$encoded = base64->encode($original);
$decoded = base64->decode($encoded);
$encoded = storable->encode($original);
$decoded = storable->decode($encoded);
# imported subroutines
$encoded = encode_base64($original);
$decoded = decode_base64($encoded);
$encoded = encode_storable($original);
$decoded = decode_storable($encoded);
You can define alternate names for codecs by providing a reference to a
hash array.
use Badger::Codecs
codecs => {
text => 'base64',
data => 'storable+base64',
};
# codec objects
$encoded = text->encode($original);
$decoded = text->decode($encoded);
$encoded = data->encode($original);
$decoded = data->decode($encoded);
# imported subroutines
$encoded = encode_text($original);
$decoded = decode_text($encoded);
$encoded = encode_data($original);
$decoded = decode_data($encoded);
=head1 IMPORTABLE SUBROUTINES
=head2 Codec()
This subroutine can be used as a shortcut to the L<codec> method.
use Badger::Codecs 'Codec';
my $yaml = Codec('YAML');
lib/Badger/Codecs.pm view on Meta::CPAN
my $codec = Badger::Codecs->codec('bam'); # Stoat::Codec::Bam
=head2 codec($type, %config)
Creates and returns a C<Badger::Codec> object for the specified
C<$type>. Any additional arguments are forwarded to the codec's
constructor method.
my $codec = Badger::Codecs->codec('storable');
my $encoded = $codec->encode($original);
my $decoded = $codec->decode($encoded);
If the named codec cannot be found then an error is thrown.
=head2 chain($type, %config)
Creates a new L<Badger::Codec::Chain> object to represent a chain of codecs.
=head2 encode($type, $data)
All-in-one method for encoding data via a particular codec.
t/codec/base64.t view on Meta::CPAN
use Badger::Test
tests => 1,
debug => 'Badger::Codec Badger::Codecs',
args => \@ARGV;
use constant Codec => 'Badger::Codec::Base64';
my $input = '**&@£foobar';
my $encode = Codec->encode($input);
my $decode = Codec->decode($encode);
is( $decode, $input, 'decoded output matches input' );
__END__
# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
t/codec/codecs.t view on Meta::CPAN
#-----------------------------------------------------------------------
# test class methods
#-----------------------------------------------------------------------
$enc = CODECS->encode(storable => $data);
ok( $enc, 'encoded data via Codec' );
$dec = CODECS->decode(storable => $enc);
ok( $dec, 'decoded data via Codec' );
is( $dec->{ message }, $data->{ message }, 'message received via Codec' );
is( $dec->{ author }, $data->{ author }, 'name transcoded via Codec' );
#-----------------------------------------------------------------------
# test object methods
#-----------------------------------------------------------------------
$codec = CODECS->codec('storable');
ok( $codec, 'got Storable codec' );
$enc = $codec->encode($data);
ok( $enc, 'encoded data' );
$dec = $codec->decode($enc);
ok( $dec, 'decoded data' );
is( $dec->{ message }, $data->{ message }, 'message received' );
is( $dec->{ author }, $data->{ author }, 'name transcoded' );
#-----------------------------------------------------------------------
# test path option
#-----------------------------------------------------------------------
$codecs = Badger::Codecs->new(
path => 'My::Codec',
);
is( $codecs->encode( foo => 'hello' ), 'FOO:hello', 'encoded foo via codecs' );
is( $codecs->decode( foo => 'FOO:world' ), 'world', 'decoded foo via codecs' );
$codec = $codecs->codec('foo');
is( $codec->encode('hello'), 'FOO:hello', 'encoded foo via codec' );
is( $codec->decode('FOO:world'), 'world', 'decoded foo via codec' );
#-----------------------------------------------------------------------
# test path option with list ref
#-----------------------------------------------------------------------
$codecs = Badger::Codecs->new(
path => ['Badger::Code', 'No::Such::Codec', 'My::Codec' ],
);
t/codec/codecs.t view on Meta::CPAN
# importing a single codec
use Badger::Codecs
codec => 'Base64';
# codec() returns a Badger::Codec::URL object
$enc = codec->encode($hello);
ok( $enc, 'encoded data via imported base64 codec()' );
$dec = codec->decode($enc);
ok( $dec, 'decoded data via imported base64 codec()' );
is( $dec, $hello, 'transcoded hello via base64 codec()' );
# encode() and decode() are imported subroutines
$enc = encode($hello);
ok( $enc, 'encoded data via imported base64 encode()' );
$dec = decode($enc);
ok( $dec, 'decoded data via imported base64 decode()' );
is( $dec, $hello, 'transcoded hello via base64 encode()/decode()' );
package Wibble2;
use Badger::Test;
# importing a single codec using list ref of arguments
use Badger::Codecs
[codec => 'storable'];
# codec() returns a Badger::Codec::URL object
$enc = codec->encode({ message => $hello });
ok( $enc, "encoded data via imported [codec => 'storable']" );
$dec = codec->decode($enc)->{ message };
ok( $dec, "decoded data via imported [codec => 'storable']" );
is( $dec, $hello, 'transcoded hello via storable encode()/decode()' );
#-----------------------------------------------------------------------
# import multiple codecs
#-----------------------------------------------------------------------
# import multiple codecs
use Badger::Codecs
codecs => 'base64 storable';
t/codec/codecs.t view on Meta::CPAN
package Somewhere::Else; # avoid redefine warnings;
use Badger::Test;
use Badger::Codecs
codec => 'storable+base64';
$enc = codec->encode($data);
ok( $enc, 'encoded data via storable+base64 chain' );
$dec = codec->decode($enc);
ok( $enc, 'decoded data via storable+base64 chain' );
is( $dec->{ message }, $hello, 'integrity check' );
is( decode(encode($data))->{ message },
$hello, 'transcoded via storable+base64 encode/decode subs' );
is( Badger::Codecs->decode(
'storable+base64' => Badger::Codecs->encode(
'storable+base64' => $data
)
t/codec/encode.t view on Meta::CPAN
our $encoded = {
'UTF-8' => "\x{ef}\x{bb}\x{bf}m\x{c3}\x{b8}\x{c3}\x{b8}se\x{e2}\x{80}\x{a6}",
'UTF-16BE' => "\x{fe}\x{ff}\x{0}m\x{0}\x{f8}\x{0}\x{f8}\x{0}s\x{0}e &",
'UTF-16LE' => "\x{ff}\x{fe}m\x{0}\x{f8}\x{0}\x{f8}\x{0}s\x{0}e\x{0}& ",
'UTF-32BE' => "\x{0}\x{0}\x{fe}\x{ff}\x{0}\x{0}\x{0}m\x{0}\x{0}\x{0}\x{f8}\x{0}\x{0}\x{0}\x{f8}\x{0}\x{0}\x{0}s\x{0}\x{0}\x{0}e\x{0}\x{0} &",
'UTF-32LE' => "\x{ff}\x{fe}\x{0}\x{0}m\x{0}\x{0}\x{0}\x{f8}\x{0}\x{0}\x{0}\x{f8}\x{0}\x{0}\x{0}s\x{0}\x{0}\x{0}e\x{0}\x{0}\x{0}& \x{0}\x{0}",
};
while (my ($enc, $original) = each %$encoded) {
$decode = Codec->decode($enc, $original);
ok( $decode, "decoded $enc via codec: $decode" );
is( $decode, $uncoded, "decoded $enc matches uncoded" );
$encode = Codec->encode($enc, $decode);
ok( $encode, "encoded $enc: " . nicely($encode) );
is( reasciify($encode), reasciify($original), "encoded $enc output matches input" );
}
sub reasciify {
my $string = shift;
$string = join '', map {
my $ord = ord($_);
t/codec/encode.t view on Meta::CPAN
our $uncoded = $main::uncoded;
our $encoded = $main::encoded;
*reasciify = \&main::reasciify;
*nicely = \&main::nicely;
*ok = \&main::ok;
*is = \&main::is;
while (my ($enc, $original) = each %$encoded) {
$decode = decode($enc, $original);
ok( $decode, "decoded $enc via decode(): $decode" );
is( $decode, $uncoded, "decode() $enc matches uncoded" );
$encode = encode($enc, $decode);
ok( $encode, "encoded $enc via encode(): " . nicely($encode) );
is( reasciify($encode), reasciify($original), "encode() $enc output matches input" );
}
__END__
t/codec/encoding.t view on Meta::CPAN
args => \@ARGV;
use constant Codec => 'Badger::Codec::Encoding';
use Encode qw();
use bytes;
my $uncoded = "Hello World";
my $encoded = Codec->encode($uncoded); # ASCII by default
is( $encoded, $uncoded, 'ASCII encoding nullop' );
my $decoded = Codec->decode($encoded);
is( $decoded, $uncoded, 'ASCII decoding nullop' );
#-----------------------------------------------------------------------
# test utf8, and various other specific encodings
#-----------------------------------------------------------------------
package Badger::Test::Encoding::utf8;
use Badger::Codecs codec => 'utf8';
use Badger::Test;
our $moose = "\x{ef}\x{bb}\x{bf}m\x{c3}\x{b8}\x{c3}\x{b8}se\x{e2}\x{80}\x{a6}";
$uncoded = Encode::decode( utf8 => $moose );
$encoded = encode($uncoded);
$decoded = decode($encoded);
is( reasciify($encoded), reasciify($moose), "encoded utf8" );
is( $decoded, $uncoded, "decoded utf8" );
sub reasciify {
my $string = shift;
$string = join '', map {
my $ord = ord($_);
($ord > 127 || ($ord < 32 && $ord != 10))
? sprintf '\x{%x}', $ord
: $_
} split //, $string;
t/codec/json.t view on Meta::CPAN
e => 2.718,
hash => {
things => [ qw( foo bar baz ) ],
}
};
my $encoded = Codec->encode($data);
ok( $encoded, 'encoded data' );
my $decoded = Codec->decode($encoded);
ok( $decoded, 'decoded data' );
is( $decoded->{ pi }, $data->{ pi }, 'pi remains constant' );
is( $decoded->{ e }, $data->{ e }, 'e remains constant' );
is( $decoded->{ hash }->{ things }->[0], 'foo', 'foo' );
__END__
# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
t/codec/storable.t view on Meta::CPAN
e => 2.718,
hash => {
things => [ qw( foo bar baz ) ],
}
};
my $encoded = Codec->encode($data);
ok( $encoded, 'encoded data' );
my $decoded = Codec->decode($encoded);
ok( $decoded, 'decoded data' );
is( $decoded->{ pi }, $data->{ pi }, 'pi remains constant' );
is( $decoded->{ e }, $data->{ e }, 'e remains constant' );
is( $decoded->{ hash }->{ things }->[0], 'foo', 'foo' );
__END__
# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
t/codec/tt.t view on Meta::CPAN
e => 2.718,
karma => -99,
hash => {
things => [ qw( foo bar baz ) ],
}
};
my $encoded = Codec->encode($data);
ok( $encoded, 'encoded data' );
my $decoded = Codec->decode($encoded);
ok( $decoded, 'decoded data' );
is( $decoded->{ pi }, $data->{ pi }, 'pi remains constant' );
is( $decoded->{ e }, $data->{ e }, 'e remains constant' );
is( $decoded->{ karma }, $data->{ karma }, 'karma is unchaged' );
is( $decoded->{ hash }->{ things }->[0], 'foo', 'foo is unchanged' );
#-----------------------------------------------------------------------
# define some more data and a comparison sub for different syntax tests
#-----------------------------------------------------------------------
$data = {
message => 'Hello World, this is some text',
things => ['a list', 'of some things'],
stuff => {
pi => 3.14,
foo => [ { nested => 'hash' }, ['nested', 'list' ] ],
},
};
sub compare_decoded_data {
my ($name, $decoded, $data) = @_;
is( $decoded->{message}, $data->{ message }, "$name message" );
is( $decoded->{things}->[0], $data->{things}->[0], "$name things 0" );
is( $decoded->{things}->[1], $data->{things}->[1], "$name things 1" );
is( $decoded->{stuff}->{pi}, $data->{stuff}->{ pi }, "$name pi" );
is( $decoded->{stuff}->{foo}->[0]->{nested}, $data->{stuff}->{foo}->[0]->{nested}, "$name foo.nested $data->{stuff}->{foo}->[0]->{nested}" );
is( $decoded->{stuff}->{foo}->[1]->[1], $data->{stuff}->{foo}->[1]->[1], "$name foo.nested $data->{stuff}->{foo}->[1]->[1]" );
}
#-----------------------------------------------------------------------
# Try it first with Perlish data syntax...
#-----------------------------------------------------------------------
$decoded = Codec->decode(<<EOF);
{
message => 'Hello World, this is some text',
things => ['a list', 'of some things'],
stuff => {
pi => 3.14,
foo => [ { nested => 'hash' }, ['nested', 'list' ] ],
},
}
EOF
ok( $decoded, 'decoded Perlish data' );
compare_decoded_data( Perlish => $decoded, $data );
#-----------------------------------------------------------------------
# ...then with reduced TT syntax...
#-----------------------------------------------------------------------
$decoded = Codec->decode(<<EOF);
{
message = 'Hello World, this is some text'
things = ['a list' 'of some things']
stuff = {
pi = 3.14
foo = [ { nested = 'hash' } ['nested' 'list' ] ]
}
}
EOF
ok( $decoded, 'decoded TTish data' );
compare_decoded_data( TTish => $decoded, $data );
#-----------------------------------------------------------------------
# ...and again with JSON syntax
#-----------------------------------------------------------------------
$decoded = Codec->decode(<<EOF);
{
message: 'Hello World, this is some text',
things: ['a list' 'of some things'],
stuff: {
pi: 3.14,
foo: [ { nested: 'hash' }, ['nested', 'list' ] ]
}
}
EOF
ok( $decoded, 'decoded JSONish data' );
compare_decoded_data( JSONish => $decoded, $data );
#-----------------------------------------------------------------------
# test different output formats
#-----------------------------------------------------------------------
$data = {
pi => 3.14,
e => 2.718,
foo => ['bar', 'baz'],
};
t/codec/unicode.t view on Meta::CPAN
our $encoded = {
'UTF-8' => "\x{ef}\x{bb}\x{bf}m\x{c3}\x{b8}\x{c3}\x{b8}se\x{e2}\x{80}\x{a6}",
'UTF-16BE' => "\x{fe}\x{ff}\x{0}m\x{0}\x{f8}\x{0}\x{f8}\x{0}s\x{0}e &",
'UTF-16LE' => "\x{ff}\x{fe}m\x{0}\x{f8}\x{0}\x{f8}\x{0}s\x{0}e\x{0}& ",
'UTF-32BE' => "\x{0}\x{0}\x{fe}\x{ff}\x{0}\x{0}\x{0}m\x{0}\x{0}\x{0}\x{f8}\x{0}\x{0}\x{0}\x{f8}\x{0}\x{0}\x{0}s\x{0}\x{0}\x{0}e\x{0}\x{0} &",
'UTF-32LE' => "\x{ff}\x{fe}\x{0}\x{0}m\x{0}\x{0}\x{0}\x{f8}\x{0}\x{0}\x{0}\x{f8}\x{0}\x{0}\x{0}s\x{0}\x{0}\x{0}e\x{0}\x{0}\x{0}& \x{0}\x{0}",
};
while (my ($enc, $original) = each %$encoded) {
$decode = Codec->decode($original);
ok( $decode, "decoded $enc via codec: $decode" );
is( $decode, $uncoded, "decoded $enc matches uncoded" );
$encode = Codec->encode($decode);
ok( $encode, "encoded $enc: " . nicely($encode) );
is( reasciify($encode), reasciify($moose), "encoded $enc output matches input" );
}
sub reasciify {
my $string = shift;
$string = join '', map {
my $ord = ord($_);
t/codec/unicode.t view on Meta::CPAN
our $moose = $main::moose;
our $uncoded = $main::uncoded;
our $encoded = $main::encoded;
*reasciify = \&main::reasciify;
*nicely = \&main::nicely;
*ok = \&main::ok;
*is = \&main::is;
while (my ($enc, $original) = each %$encoded) {
$decode = decode($original);
ok( $decode, "decoded $enc via decode(): $decode" );
is( $decode, $uncoded, "decode() $enc matches uncoded" );
$encode = encode($decode);
ok( $encode, "encoded $enc via encode(): " . nicely($encode) );
is( reasciify($encode), reasciify($moose), "encode() $enc output matches input" );
}
__END__
t/codec/uri.t view on Meta::CPAN
tests => 2,
debug => 'Badger::Codec::URI',
args => \@ARGV;
use Badger::Codecs
codec => 'uri';
my $uncoded = 'Hello World&^%';
my $encoded = encode($uncoded);
is( $encoded, 'Hello%20World%26%5E%25', 'URI encoded data' );
my $decoded = decode($encoded);
is( $decoded, $uncoded, 'decoded output matches input' );
__END__
# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
t/codec/url.t view on Meta::CPAN
tests => 2,
debug => 'Badger::Codec::URL',
args => \@ARGV;
use Badger::Codecs
codec => 'url';
my $uncoded = 'http://badgerpower.com/example?message="Hello World"';
my $encoded = encode($uncoded);
is( $encoded, 'http://badgerpower.com/example?message=%22Hello%20World%22', 'URL encoded data' );
my $decoded = decode($encoded);
is( $decoded, $uncoded, 'decoded output matches input' );
__END__
# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
t/core/class.t view on Meta::CPAN
#-----------------------------------------------------------------------
package Test::Codec1;
use Badger::Test;
use Badger::Class codec => 'base64';
my $enc = encode('Hello World');
is( $enc, "SGVsbG8gV29ybGQ=\n", 'encoded base64' );
my $dec = decode($enc);
is( $dec, 'Hello World', 'decoded base64' );
#-----------------------------------------------------------------------
# test method() method
#-----------------------------------------------------------------------
package Test::Method1;
use Badger::Class
base => 'Badger::Base',
import => 'class';