SNMP::Class::OID - Represents an SNMP Object-ID.


 use SNMP::Class::OID;

 #create an object
 my $oid = SNMP::Class::OID->new('.');
 my $oid = SNMP::Class::OID->new('sysName.0');
 #overloaded scalar representation
 print $oid; # evaluates to sysName.0

 $oid->to_string; #string representation -- sysName.0
 $oid->numeric; #numeric representation -- .
 $oid->to_array; #(1,3,6,1,2,1,1,5,0)
 $oid->[1]; #can be used as array reference -- returns 5
 $oid->length; #9

 my $oid2 = $oid->slice(3,6); #new object : .
 my $oid2 = $oid->slice(3..6); #same

 $oid1 == $oid2; # yields true if they are the same
 $oid1 == '.' #also acceptable, second operand will be converted 

 $oid2 = SNMP::Class::OID->new('.');
 $oid2->contains($oid); #true; Because . is under .
 $oid2->contains('.'); #also true, string autoconverted to SNMP::Class::OID

 SNMP::Class::OID(".1.3.6") . SNMP::Class::OID("1.2.1"); #returns .
 SNMP::Class::OID(".1.3.6") . '.1.2.1'; #also acceptable, returns the same


overloaded operators

The following operators are overloaded:

  • <=>

    Two SNMP::Class::OID objects can be compared using the == operator. The result is what everybody expects.

  • '+'

    Two SNMP::Class::OID objects can be concatenated using the + operator. Note that order actually is important. Example: .1.3.6 + .1.4.1 will yield .

  • @{}

    If an SNMP::Class::OID object is used as an array reference, it will act as an array containing the individual numbers of the OID. Example:

     my $oid = SNMP::Class::OID->new("");
     print $oid->[1]; #will print 3 


new can be used to construct a new object-id. Takes one string as an argument, like ".". Returns an SNMP::Class::OID object, or confesses if that is not possible. If the 1rst argument is a NetSNMP::OID instead of a string, the constructor will notice and take appropriate action to return a valid object.


Returns, if it exists, the SNMP SYNTAX clause for the oid or undef if it doesn't.


Tells if we know the syntax for the object. Convenience shortcut instead of testing get_syntax for definedness.


Returns the label for this oid if it exists or undef if it doesn't.


Returns an SNMP::Class::OID object corresponding to the appropriate object-id. For example, for an oid like ifDescr.3, we would get a new SNMP::Class::OID equivalent to ifDescr. May return undef, as the label may not be found in the loaded MIBs.


Tells if there is a label for the object. Convenience shortcut instead of testing get_label_oid for definedness.


Returns an SNMP::Class::OID object corresponding to the instance of this oid. For example, for an oid like ifDescr.3, we would get a new SNMP::Class::OID equivalent to .3. May return undef, as there may be no instance (for example a non-leaf oid) or it may not be possible to know it.


Tells if there is an instance for the object. Convenience shortcut instead of testing get_instance_oid for definedness.


Slice can extract a portion of an object-id and return it as a new SNMP::Class::OID object. Example:

 my $oid = SNMP::Class::OID->new("");
 my $suboid = $oid->slice(1..3); #will return .1.3.6
 my $suboid = $oid->slice(1,2,3); #completely equivalent
 my $suboid = $oid->slice(1,3); #also completely equivalent

To extract a single number from the object-id you can simply say for example:

 my $suboid = $oid->slice(2);


Returns an array representation of the object OID.


Returns the length (in items) of the object OID.


returns true if the object represents the null object identifier. SNMPv2-SMI defines a null object id to be { 0 0 } or 0.0 or zeroDotZero. Let's just hope that we won't encounter 0.0 instances any time soon.


Returns a numeric representation of the object.


Returns a string representation of the object. Difference with numeric is that numeric always returns numbers like ., while this method may return strings like "sysName.0" etc.


Concatenates two OIDs. Use it through the . overloaded operator. Second argument can be a string, will be autoconverted to SNMP::Class::OID before addition. If one of the arguments is 0.0, the result should be equal to the other.


Compares two OIDs. Has the same semantic with the spaceship <=> operator. Second argument can also be a string. You probably will never use that method explicitly, only through the overloaded operators <,>,==,!= etc. See also the is_equal method.


Returns 1 if the 1st argument is the same oid, else undef.


Can ascertain if an oid is a subset of the oid represented by the object. Takes SNMP::Class::OID as 1st and only argument. String also acceptable as it will be autoconverted. Example:

 $oid1 = SNMP::Class::OID->new(".");
 $oid2 = SNMP::Class::OID->new(".");
 $oid1->contains($oid2); #yields true
 $oid1->contains(".");#the same


Can create an oid from a literal string. Useful to generate instances which correspond to strings. 1st argument is the string to represent with an OID. If the 2nd argument is there and is true, the SNMP octet-string is assumed to be IMPLIED, thus the first number which represents the length of the string is missing. Example:

 my $instance = SNMP::Class::OID->new_from_string("foo"); # returns .


 my $instance = SNMP::Class::OID->new_from_string("foo","yes_it_is_implied"); # returns .102.111.111


Athanasios Douitsis, <aduitsis at>


Please report any bugs or feature requests to bug-snmp-class-oid at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc SNMP::Class

You can also look for information at:


Since I am using NetSNMP::OID internally, my gratitude goes to the fine folks that gave us the original SNMP module. Many thanks to all.


Copyright 2008 Athanasios Douitsis, all rights reserved.

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