# phpman > perldoc > Data::Serializer::Cookbook

## NAME
    Cookbook - Examples of how to use [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown)

## DESCRIPTION
    [Data::Serializer::Cookbook](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ACookbook/markdown) is a collection of solutions for using [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown).

## CONVENTIONS
    Unless otherwise specified, all examples can be assumed to begin with:

      use [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown);

      my $serializer = [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown)->new();

    Some examples will show different arguments to the new method, where specified simply use that
    line instead of the simple form above.

CONVENTIONS for Raw Access
    Fort hose who want a straight pass through to the underlying serializer, where nothing else is
    done (no encoding, encryption, compression, etc) there is [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown)::[Raw(3)](https://www.chedong.com/phpMan.php/man/Raw/3/markdown).

    These begin like this:

      use [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown);

      my $raw_serializer = [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown)->new();

## Encrypting your data
    You wish to encrypt your data structure, so that it can only be decoded by someone who shares
    the same key.

### Solution
      $serializer->secret('mysecret');

      my $encrypted_hashref = $serializer->serializer($hash);

      ... (in other program) ...

      $serializer->secret('mysecret');

      my $clear_hash = $serializer->deserializer($encrypted_hash);

    Note: You will have to have the [Crypt::CBC](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ACBC/markdown) module installed for this to work.

## Compressing your data
    You wish to compress your data structure to cut down on how much disk space it will take up.

### Solution
      $serializer->[compress(1)](https://www.chedong.com/phpMan.php/man/compress/1/markdown);

      my $compressed_hashref = $serializer->serializer($hash);

      ... (in other program) ...

      my $clear_hash = $serializer->deserializer($compressed_hash);

    Note: You will have to have the [Compress::Zlib](https://www.chedong.com/phpMan.php/perldoc/Compress%3A%3AZlib/markdown) module installed for this to work. Your mileage
    will vary dramatically depending on what serializer you use. Some serializers are already fairly
    compact.

## You want to read in data serialized outside of [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown)
    You need to write a program that can read in data serialized in a format other than
    [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown). For example you need to be able to be able to process data serialized by
    [XML::Dumper](https://www.chedong.com/phpMan.php/perldoc/XML%3A%3ADumper/markdown).

### Solution
      use [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown);

      my $xml_raw_serializer = [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown)->(serializer => '[XML::Dumper](https://www.chedong.com/phpMan.php/perldoc/XML%3A%3ADumper/markdown)');

      my $hash_ref = $xml_raw_serializer->deserialize($xml_data);

## You want to write serialized data in a form understood outside of [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown)
    You need to write a program that can write out data in a format other than [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown). Or
    said more generically you need to write out data in the format native to the underlying
    serializer. For our example we will be exporting data using [XML::Dumper](https://www.chedong.com/phpMan.php/perldoc/XML%3A%3ADumper/markdown) format.

### Solution
      ues [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown);

      my $xml_raw_serializer = [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown)->(serializer => '[XML::Dumper](https://www.chedong.com/phpMan.php/perldoc/XML%3A%3ADumper/markdown)');

      my $xml_data = $xml_raw_serializer->serialize($hash_ref);

## You want to convert data between two different serializers native formats
    You have data serialized by php that you want to convert to xml for use by other programs.

### Solution
      use [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown);

      my $xml_raw_serializer = [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown)->(serializer => '[XML::Dumper](https://www.chedong.com/phpMan.php/perldoc/XML%3A%3ADumper/markdown)');

      my $php_raw_serializer = [Data::Serializer::Raw](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer%3A%3ARaw/markdown)->(serializer => '[PHP::Serialization](https://www.chedong.com/phpMan.php/perldoc/PHP%3A%3ASerialization/markdown)');

      my $hash_ref = $php_raw_serializer->deserialize($php_data);

      my $xml_data = $xml_raw_serializer->serialize($hash_ref);

Keeping data persistent between executions of a program.
    You have a program that you run every 10 minutes, it uses SNMP to pull some counters from one of
    your routers. You want your program to keep the counters from the last run so that it can see
    how much traffic has passed over a link since it last ran.

### Solution
      # path to store our serialized data
      # be paranoid, use full paths
      my $last_run_datafile = '/full/path/to/file/lastrun.data';

      #We keep our data as a hash reference
      my $last_data = $serializer->retrieve($last_run_datafile);

      #Pull in our new data through 'pull_data()';
      my $new_data = query_router($router);

      #run comparison code
      run_comparison($last_data,$new_data);

      $serializer->store($new_data);

## AUTHOR
    Neil Neely <<neil@neely.cx>>.

## COPYRIGHT
    Copyright (c) 2001-2011 Neil Neely. All rights reserved.

    This program is free software; you can redistribute it and/or modify it under the same terms as
    Perl itself.

## SEE ALSO
    Data::[Serializer(3)](https://www.chedong.com/phpMan.php/man/Serializer/3/markdown)
    [Data::Serializer](https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ASerializer/markdown)::[Raw(3)](https://www.chedong.com/phpMan.php/man/Raw/3/markdown)

