Encoding-BER

 view release on metacpan or  search on metacpan

eg/snmpget  view on Meta::CPAN


# open udp socket
my $ip  = gethostbyname($host);
die "cannot resolve hostname: $host\n" unless $ip;
my $sin = sockaddr_in($PORT, $ip);

socket(SNMP, PF_INET, SOCK_DGRAM, 0) || die "socket failed: $!\n";


my $enc  = Encoding::BER::SNMP->new(
               decoded_callback => sub { bless $_[1], 'MyBER::Result' },
               );

# build SNMPv1 pkt
my $snmp = $enc->encode( [ 0, # versn
			   { type => 'string', value => $comm },
			   { type  => 'get_request',
			     value => [ 1, # reqid
					0, # errst
					0, # erridx
					[ [ { type => 'oid', value => $oid }, undef ]]] }]);

eg/snmpget  view on Meta::CPAN

    }elsif( $val->{identval} == 0x82 ){
	$val = '#<endOfMibView>';
    }else{
	$val = $val->{value};
    }

    print "$oid\t$val\n";
}
exit;

# (recursively) map the decoded results onto a flat hash
sub MyBER::Result::value_map {
    my $me  = shift;
    my $res = shift;
    my $tpl = shift;

    unless( ref $tpl ){
	$res->{$tpl} = $me;
	return $res;
    }
    unless( ref $me->{value} ){

lib/Encoding/BER.pm  view on Meta::CPAN

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

=head1 DESCRIPTION

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.
    
=head1 METHODS

=over 4

=cut
    ;

lib/Encoding/BER.pm  view on Meta::CPAN

    
=item 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" }
    

=item 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 }
    
=item 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

=back

lib/Encoding/BER.pm  view on Meta::CPAN

	if( $pretty ){
	    $val = $pretty->( $me, $val ) || $val;
	}
	$result = $val;
    }    

    $result->{type}     = $typdat;
    $result->{tagnum}   = $tagnum;
    $result->{identval} = $typval;
    
    if( my $c = $me->{decoded_callback} ){
	$result = $c->( $me, $result ) || $result;  # make sure the brain hasn't fallen out
    }
    return( $result, $tlen );
}

sub app_tag_data_bynumber {
    my $me    = shift;
    my $class = shift;
    my $tnum  = shift;
    



( run in 0.228 second using v1.01-cache-2.11-cpan-0d8aa00de5b )