Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  ENCODING::BER (3)

.ds Aq ’


Encoding::BER - Perl module for encoding/decoding data using ASN.1 Basic Encoding Rules (BER)



  use Encoding::BER;
  my $enc = Encoding::BER->new();
  my $ber = $enc->encode( $data );
  my $xyz = $enc->decode( $ber );


Unlike many other BER encoder/decoders, this module uses tree structured data as the interface to/from the encoder/decoder.

The decoder does not require any form of template or description of the data to be decoded. Given arbitrary BER encoded data, the decoder produces a tree shaped perl data structure from it.

The encoder takes a perl data structure and produces a BER encoding from it.


new(option => value, ...) constructor.

    my $enc = Encoding::BER->new( error => sub{ die "$_[1]\n" } );

the following options are available:
error coderef called if there is an error. will be called with 2 parameters, the Encoding::BER object, and the error message.

    # example: die on error
    error => sub{ die "oops! $_[1]\n" }

warn coderef called if there is something to warn about. will be called with 2 parameters, the Encoding::BER object, and the error message.

    # example: warn for warnings
    warn => sub{ warn "how odd! $_[1]\n" }

decoded_callback coderef called for every element decoded. will be called with 2 parameters, the Encoding::BER object, and the decoded data. [see DECODED DATA]

    # example: bless decoded results into a useful class
    decoded_callback => sub{ bless $_[1], MyBER::Result }

debug boolean. if true, large amounts of useless gibberish will be sent to stderr regarding the encoding or decoding process.

    # example: enable gibberish output
    debug => 1

add_implicit_tag(class, type, tag-name, tag-number, base-tag) add a new tag similar to another tag. class should be one of universal, application, context, or private. type should be either primitive or contructed. tag-name should specify the name of the new tag. tag-number should be the numeric tag number. base-tag should specify the name of the tag this is equivalent to.

    example: add a tagged integer
    in ASN.1: width-index ::= [context 42] implicit integer
    $ber->add_implicit_tag(context, primitive, width-index, 42, integer);

encode( data ) BER encode the provided data. [see: ENCODING DATA]

  my $ber = $enc->encode( [0, public, [7.3, 0, 0, [foo, bar]]] );

decode( ber ) Decode the provided BER encoded data. returns a perl data structure. [see: DECODED DATA]

  my $data = $enc->decode( $ber );


You can give data to the encoder in either of two ways (or mix and match).

You can specify simple values directly, and the module will guess the correct tags to use. Things that look like integers will be encoded as integer, things that look like floating-point numbers will be encoded as real, things that look like strings, will be encoded as octet_string. Arrayrefs will be encoded as sequence.

  $enc->encode( [0, 1.2, "foobar", [ "baz", 37.94 ]] );

Alternatively, you can explicity specify the type using a hashref containing type and value keys.

  $enc->encode( { type  => sequence,
                  value => [
                             { type  => integer,
                               value => 37 } ] } );

The type may be specfied as either a string containg the tag-name, or as an arryref containing the class, type, and tag-name.

  type => octet_string
  type => [universal, primitive, octet_string]

Note: using the second form above, you can create wacky encodings that no one will be able to decode.

The value should be a scalar value for primitive types, and an arrayref for constructed types.

  { type => octet_string, value => foobar }
  { type => set, value => [ 1, 2, 3 ] }

  { type  => [universal, constructed, octet_string],
    value => [ foo, bar ] }


The values returned from decoding will be similar to the way data to be encoded is specified, in the full long form. Additionally, the hashref will contain: identval the numeric value representing the class+type+tag and tagnum the numeric tag number.

  a string might be returned as:
  { type     => [universal, primitive, octet_string],
    identval => 4,
    tagnum   => 4,
    value    => foobar,


The following are recognized as valid names of tags:

    bit_string bmp_string bool boolean character_string embedded_pdv
    enum enumerated external float general_string generalized_time
    graphic_string ia5_string int int32 integer integer32 iso646_string
    null numeric_string object_descriptor object_identifier octet_string
    oid printable_string real relative_object_identifier relative_oid
    roid sequence sequence_of set set_of string t61_string teletex_string
    uint uint32 universal_string universal_time unsigned_int unsigned_int32
    unsigned_integer utf8_string videotex_string visible_string


If you have Math::BigInt, it can be used for large integers. If you want it used, you must load it yourself:

    use Math::BigInt;
    use Encoding::BER;

It can be used for both encoding and decoding. The encoder can be handed either a Math::BigInt object, or a big string of digits marked as an integer:

    use math::BigInt;

    my $x = Math::BigInt->new( 12345678901234567890 );
    $enc->encode( $x )

    $enc->encode( { type => integer, 12345678901234567890 } );

During decoding, a Math::BigInt object will be created if the value looks big.


By default, this module exports nothing. This can be overridden by specifying something else:

    use Encoding::BER (import, hexdump);


If your application uses the same tag-number for more than one type of implicitly tagged primitive, the decoder will not be able to distinguish between them, and will not be able to decode them both correctly. eg:

    width ::= [context 12] implicit integer
    girth ::= [context 12] implicit real

If you specify data to be encoded using the short form, the module may guess the type differently than you expect. If it matters, be explicit.

This module does not do data validation. It will happily let you encode a non-ascii string as a ia5_string, etc.


If you wish to use reals, the POSIX module is required. It will be loaded automatically, if needed.

Familiarity with ASN.1 and BER encoding is probably required to take advantage of this module.


    Yellowstone National Park
    Encoding::BER::CER, Encoding::BER::DER
    Encoding::BER::SNMP, Encoding::BER::Dumper
    ITU-T x.690


    Jeff Weisberg -

Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 ENCODING::BER (3) 2015-12-15

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.