NetAddr::IP::LazyInit - NetAddr::IP objects with deferred validation SEE DESCRIPTION BEFORE USING




    use NetAddr::IP::LazyInit;

    my $ip = new NetAddr::IP::LazyInit( '' );


This module is designed to quickly create objects that may become NetAddr::IP objects. It accepts anything you pass to it without validation. Once a method is called that requires operating on the value, the full NetAddr::IP object is constructed.

You can see from the benchmarks that once you need to instantiate NetAddr::IP the speed becomes worse than if you had not used this module. What I mean is that this adds unneeded overhead if you intend to do IP operations on every object you create.


Because validation is deferred, this module assumes you will only ever give it valid data. If you try to give it anything else, it will happily accept it and then die once it needs to inflate into a NetAddr::IP object.


This module was inspired by discussion with Jan Henning Thorsen, <jhthorsen at>, and example code he provided. The namespace and part of the documentation/source is inspired by DateTime::LazyInit by Rick Measham, <>

I didn't have to do much so I hate to take author credit, but I am providing the module, so complaints can go to me.

Robert Drake, <>


If we could actually load NetAddr::IP objects in the background while nothing is going on that would be neat. Or we could create shortcut methods when the user knows what type of input he has. new_from_ipv4('ip','mask'). We might be able to use Socket to build the raw materials and bless a new NetAddr::IP object without going through it's validation.


Copyright (C) 2014 by Robert Drake

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.



This replaces the NetAddr::IP->new method with a stub that stores the arguments supplied in a temporary variable and returns immediately. No validation is performed.

Once you call a method that can't be handled by LazyInit, a full NetAddr::IP object is built and the request passed into that object.

   my $ip = NetAddr::IP::LazyInit->new("");


Returns the IP address of the object. If we can extract the IP as a string without converting to a real NetAddr::IP object, then we return that. Currently it only returns IPv6 strings in lower case, which may break your application if you aren't using the new standard.

    my $ip = NetAddr::IP::LazyInit->new("");
    print $ip->addr;


Returns the subnet mask of the object. If the user used the two argument option then it returns the string they provided for the second argument. Otherwise this will inflate to build a real NetAddr::IP object and return the mask.

    my $ip = NetAddr::IP::LazyInit->new("", "");
    print $ip->mask;