Net-DHCPv6

 view release on metacpan or  search on metacpan

lib/Net/DHCPv6/OptionList.pm  view on Meta::CPAN

#!/bin/false
# ABSTRACT: Shared container for a collection of DHCPv6 options
# PODNAME: Net::DHCPv6::OptionList
use strictures 2;

package Net::DHCPv6::OptionList;
$Net::DHCPv6::OptionList::VERSION = '0.003';
use Net::DHCPv6::Option::Generic ();
use Carp                         qw( croak );
use Ref::Util                    qw( is_ref );
use namespace::clean;

my $EMPTY        = q();
my $OPT_HDR_SIZE = 4;     ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)

our %OPTION_CLASS;

sub new {
    my $class = shift;
    return bless {
        options_by_code => {},
        options_order   => [],
    }, $class;
}

sub add_option {
    my ( $self, $option ) = @_;
    my $code = $option->code;
    push @{ $self->{options_order} }, $code
        unless exists $self->{options_by_code}{$code};
    $self->{options_by_code}{$code} //= [];
    return push @{ $self->{options_by_code}{$code} }, $option;
}

sub get_option {
    my ( $self, $code ) = @_;
    my $list = $self->{options_by_code}{$code};
    return unless $list && @{$list};
    return $list->[0];
}

sub remove_option {
    my ( $self, $code ) = @_;
    delete $self->{options_by_code}{$code};
    return @{ $self->{options_order} } = grep { $_ != $code } @{ $self->{options_order} };
}

sub options {
    my $self = shift;
    return [] unless @{ $self->{options_order} };
    my @opts;
    for my $code ( @{ $self->{options_order} } ) {
        my $list = $self->{options_by_code}{$code};
        push @opts, @{$list} if $list;
    }
    return \@opts;
}

sub as_bytes {
    my $self     = shift;
    my $opt_list = $self->options or return $EMPTY;
    return join( $EMPTY, map { $_->as_bytes } @{$opt_list} );
}

sub try_from_bytes {
    my ( $class, $bytes ) = @_;
    return ( $class->new, undef ) unless defined $bytes && CORE::length( $bytes );

    my $list   = $class->new;
    my $offset = 0;



( run in 1.593 second using v1.01-cache-2.11-cpan-6aa56a78535 )